0

I've requirement to parse and update some of the field values in nested json as below

What is the best way to do with Java ?

I googled but didn't find any simple & effective way to do the same

I did refer to question How to parse JSON in Java, but answers given there are quite abstract & also doesn't explain how to update particular value against nested key

[   {
    "applicationId": "email-id-1202",
    "extRefNumber": "SALARY_STATEMENT-ref-number2018-04-06T17:32:01.852",
    "bankName": "SALARY_STATEMENT",
    "applicants": [
      {
        "personId": "1",
        "documents": [
          {
            "fileId": "2",
            "documentId": "2",
            "type": "TAX_DOCUMENTS"
          }
        ]
      }
    ],
    "data": {
      "firstName": "First",
      "lastName": "Last",
      "branchCode": "branch-code"
    }  ]
vikramvi
  • 3,312
  • 10
  • 45
  • 68
  • Parse the Json using Google Gson or Jackson libraries, make your modification, re-serialize into Json. That's probably the safest way. – Thomas Timbul Apr 09 '18 at 13:07
  • @ThomasTimbul can you please point me to an working example ? – vikramvi Apr 09 '18 at 13:09
  • there are tons of library for json usage in java. just pick one of them and start cracking – nafas Apr 09 '18 at 13:09
  • There will be many examples in the documentation of the above mentioned libraries, both of which are open source. – Thomas Timbul Apr 09 '18 at 13:44
  • **com.jayway.jsonpath** might work for you. With simple lines like `DocumentContext doc = JsonPath.parse(Obj.toString()); doc.set("[0].applicants[0].documents[0].fileId", 3); newObj = new JSONArray(doc.jsonString());` you should be able to update `fileId`. But beware it has two known issues e.g https://github.com/json-path/JsonPath/issues/272, & issues/375. Using old version like 0.8.1 might work around that. – papigee Jun 04 '19 at 19:48

0 Answers0