Search code examples
javajsondata-bindingjacksonfasterxml

How to add an ObjectNode into another ObjectNode as a child using Jackson?


I have the below ObjectNode.

handlerObjectNode -> {"Info":{"Brand":{"BrandName":"TOP OF THE WORLD"}}}

I have another ObjectNode in the following format.

fieldObjects -> {"Description":"REGULAR BR"}

How can I create the below ObjectNode from the above two?

{
   "Info": {
       "Brand": {
           "BrandName": "TOP OF THE WORLD"
       }
   "Description": "REGULAR BR"
   }
 }

I tried the below code.

handlerObjectNode.setAll(fieldObjects);

But it results in the following ObjectNode.

{
   "Info": {
       "Brand": {
           "BrandName": "TOP OF THE WORLD"
       }
   },
   "Description": "REGULAR BR"
 }

I am using the com.fasterxml.jackson.databind.node.ObjectNode from Jackson. Any help would be much appreciated.


Solution

  • Try This,

      root.with("Info").put("Description", "REGULAR BR");
    

    For more info, click here.