'How to update few fields in JSON file and again read the JSON with updated values in selenium using Java

How to update few fields in JSON file and again read the JSON with updated values in selenium using Java.

Below is my JSON : I want to update

{
"DemoFileJson": [
{
  "name": "demoPan",
  "Type": "patientcare",
  "indication": "fever",
  "disease": {
    "code": "282292002",
    "label": "Uncertain diagnosis"
  },

  "identified": false,

  "specimens": [
    {
      "name": "primarySpecimen",
      "type": {
        "code": "442524009",
        "label": "a.m. specimen"
      },
      "studyIdentifier": "Stud1",
      "studySubjectIdentifier": "Part1",
      "accessionNumber": "TSO500-MJ678",
      "dateAccessioned": "2019-03-29T20:00:00Z",
      "datecollected": "2019-03-29T20:00:00Z",
      "dateReceived": "2019-03-29T20:00:00Z"
    }
  ]
}
]
}


Solution 1:[1]

search & update value json file, with java class

example json file
[{"trx_id":"123"},{"trx_id":"123","datetime_expired":"20221102"}]

public class cariJSON {
    public static void main(String[] args)throws Exception {
         JSONArray dd = updateJson("datetime_expired", "20331001");    
             try (FileWriter file = new FileWriter("C://file.json")) { //store data
            file.append(dd.toString());
            file.flush();
             }
            System.out.println(dd);
   }
    
    public static JSONArray updateJson(String keyString, String newValue) throws Exception {
            
      String jsons = readFileAsString("C://file.json");
      JSONArray jsonArray = new JSONArray(jsons);  
      for (int j = 0; j < jsonArray.length(); j++)
      {
           JSONObject obj = new JSONObject(jsonArray.get(j).toString());
            // get the keys of json object
         
            if (obj.get("trx_id").equals("123"))
        {
            
            Iterator iterator = obj.keys();
            String key = null;
            while (iterator.hasNext()) {
                key = (String) iterator.next();
                // if the key is a string, then update the value
                if ((obj.optJSONArray(key) == null) && (obj.optJSONObject(key) == null)) {
                    if ((key.equals(keyString))) {

                        obj.put(key, newValue);
                        jsonArray.remove(j);
                        return jsonArray.put(obj);                 
                    }

                }
                // if it's jsonobject
                if (obj.optJSONObject(key) != null) {
                    updateJson(keyString, newValue);
                }
                // if it's jsonarray
                if (obj.optJSONArray(key) != null) {
                    JSONArray jArray = obj.getJSONArray(key);
                    for (int i = 0; i < jArray.length(); i++) {
                        updateJson(keyString, newValue);
                    }
                }
            }
        }
            System.out.println("Output array : " + jsonArray);
            return jsonArray;
           }
        return null;            
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nurfiqih