Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson, read/write nested properties

Tags:

java

json

jackson

Using Jackson, I'm creating a Json object like this:

{"A": {
    "B": {
        "C": {
            "D": 1
        }
    }
}}

with the next code

ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
ObjectNode aNode = JsonNodeFactory.instance.objectNode();
ObjectNode bNode = JsonNodeFactory.instance.objectNode();
ObjectNode cNode = JsonNodeFactory.instance.objectNode();
cNode.put("D", 1);
bNode.set("C", cNode);
aNode.set("B", bNode);
rootNode.set("A", aNode);

Is there a more simple way to create it? something like:

ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
rootNode.set("A/B/C/D",1);

Also reading a similar object is there a simply way to get the value of "D"?

like image 424
rvazquezglez Avatar asked Jan 28 '26 14:01

rvazquezglez


1 Answers

Yes, you can create nested structures with this:

ObjectNode root = JsonNodeFactory.instance.objectNode();
root.with("A").with("B").with("C").put("D", 1);
like image 197
imarban Avatar answered Jan 31 '26 05:01

imarban



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!