jsonata-java is agnostic and works with any JSON library.
Consider the following JSON data:
{
"example": [
{
"value": 4
},
{
"value": 7
},
{
"value": 13
}
]
}
The internal JSON representation is based on Map for JSON objects and List for JSON arrays/collections.
This is the format jsonata-java operates on when evaluating expressions. This format has to be used when JSON is fed as input into the evaluation function.
var data = Map.of("example",
List.of(
Map.of("value", 4),
Map.of("value", 7),
Map.of("value", 13)
)
);
jsonata-java comes with built-in support for decoding and stringifying JSON:
import com.dashjoin.jsonata.json.Json;
// jsonInput is a String or Reader:
var data = Json.parseJson(jsonInput);
var expression = jsonata("$sum(example.value)");
var result = expression.evaluate(data); // returns 24
To output JSON, you can use the Functions.string
function:
boolean beautify = true;
String toString = Functions.string(json, beautify);
See these complete samples for both internal format and JSON parsing cases.
Jackson integration is straightforward, just import the JSON as Object.class
to feed it to jsonata-java:
var data = new ObjectMapper().readValue(json, Object.class);
var expression = jsonata("$sum(example.value)");
var result = expression.evaluate(data); // returns 24
See this complete sample.
GSON integration is straightforward, just import the JSON as Object.class
to feed it to jsonata-java:
Gson gson = new Gson();
var data = gson.fromJson(json, Object.class);
var expression = jsonata("$sum(example.value)");
var result = expression.evaluate(data); // returns 24
See this complete sample.