I'm building a complex JSON object with Jackson and I want to structure the code to display the JSON structure clearly, so I'm trying to use the JsonNode API with chained with
, put
and set
methods. At one place inside all this chained stuff I need to add two arrays one after the other. Here's a simplified excerpt:
com.fasterxml.jackson.databind.ObjectNode json = new ObjectNode(JsonNodeFactory.instance);
json.with("data")
.set("array1", arrayNode1)
.set("array2", arrayNode1);
I want it to create this JSON:
{
"data": {
"array1": [...],
"array2": [...]
}
}
First set
is fine because it's called on ObjectNode
. The problem is it returns JsonNode
, which doesn't have a set
method, and so the second set
call results in compilation error.
How can I chain setting two arrays?