Java jackson – update array in json

I am new to java jackson. I want to parse through a json String and replace one of the array’s present in it (whose size is 1) with the array of size 2 by copying the same contents of the array value. Eg: Input json file

{   "order_info": {     "order_number": "123456",     "order_date": "2019-11-08T10:23:29.502Z",     "attributes": {       "checkout_locale": "en_US"     },     "order_items": [       {         "categories": [           "Clothing"         ],         "is_backordered": false,         "vendors": [           {             "name": "zxcx"           }         ],         "discount_amount": 0,         "final_sale_date": "2019-03-01T20:07:16Z"       }     ]   } } 

Output json required:

{   "order_info": {     "order_number": "123456",     "order_date": "2019-11-08T10:23:29.502Z",     "attributes": {       "checkout_locale": "en_US"     },     "order_items": [       {         "categories": [           "Clothing"         ],         "is_backordered": false,         "vendors": [           {             "name": "zxcx"           }         ],         "discount_amount": 0,         "final_sale_date": "2019-03-01T20:07:16Z"       },       {         "categories": [           "Clothing"         ],         "is_backordered": false,         "vendors": [           {             "name": "zxcx"           }         ],         "discount_amount": 0,         "final_sale_date": "2019-03-01T20:07:16Z"       }     ]   } } 

I tried code as below :

   ObjectNode objectNode = (ObjectNode) mapper.readTree(ordersApiBasePayload);    ArrayNode nodeArray = (ArrayNode) jsonNode.get("order_info").get("order_items");    ArrayNode items = objectNode.putArray("order_items");    items.add(nodeArray.get(0));    items.add(nodeArray.get(0)); 

But, by doing it this way, i do not get the existing order_items replaced with new two order_items. I get the new two order_items appended at the end of the json like below:

{   "order_info": {     "order_number": "123456",     "order_date": "2019-11-08T10:23:29.502Z",     "attributes": {       "checkout_locale": "en_US"     },     "order_items": [       {         "categories": [           "Clothing"         ],         "is_backordered": false,         "vendors": [           {             "name": "zxcx"           }         ],         "discount_amount": 0,         "final_sale_date": "2019-03-01T20:07:16Z"       }     ]   },   "order_items": [     {       "categories": [         "Clothing"       ],       "is_backordered": false,       "vendors": [         {           "name": "zxcx"         }       ],       "discount_amount": 0,       "final_sale_date": "2019-03-01T20:07:16Z"     },     {       "categories": [         "Clothing"       ],       "is_backordered": false,       "vendors": [         {           "name": "zxcx"         }       ],       "discount_amount": 0,       "final_sale_date": "2019-03-01T20:07:16Z"     }   ] } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.