How to get and update json array element without going through all json

I have a very complex json structure. It contains many array elements , and these array elements contain other array elements, etc. See json tree structure below.

Json-1 tree structure:

enter image description here

Json Tree-2 structure:

enter image description here

As indicated above in yellow, I want to update the value of the " rdKey " field. I wrote the code below and it works fine fine:

 String json = "escaped string (as it a big string, I can't put it here)"; JSONObject jsonObj = new JSONObject(json); if (jsonObj.has("responseMap")) { JSONObject responseMap = jsonObj.getJSONObject("responseMap"); if (responseMap.has("ValueJson")) { JSONObject valueJson = responseMap.getJSONObject("ValueJson"); if (valueJson.has("ticketBean_CM")) { JSONObject ticketBean_CM = valueJson.getJSONObject("ticketBean_CM"); if (ticketBean_CM.has("addByGamma")) { String addByGamma = ticketBean_CM.getString("addByGamma"); System.out.println(addByGamma); if (addByGamma.equals("VCE")) { if (responseMap.has("ScreenJson")) { JSONObject screenJson = responseMap.getJSONObject("ScreenJson"); if (screenJson.has("sections")) { JSONArray sectionArray1 = screenJson.getJSONArray("sections"); if (sectionArray1.length() > 0) { JSONObject section0 = sectionArray1.getJSONObject(0); if (section0.has("sections")) { JSONArray sectionArray2 = section0.getJSONArray("sections"); if (sectionArray2.length() > 3) { JSONObject section6 = sectionArray2.getJSONObject(3); if (section6.has("sections")) { JSONArray sectionArray3 = section6.getJSONArray("sections"); if (sectionArray3.length() > 1) { JSONObject section8 = sectionArray3.getJSONObject(1); if (section8.has("elements")) { JSONArray elementsArray1 = section8 .getJSONArray("elements"); if (elementsArray1.length() > 0) { JSONObject elements1 = elementsArray1.getJSONObject(0); if (elements1.has("elements")) { JSONArray elementsArray2 = elements1 .getJSONArray("elements"); if (elementsArray2.length() > 4) { JSONObject elements2 = elementsArray2 .getJSONObject(4); if (elements2.has("rdKey")) { System.out.println( elements2.getString("rdKey")); elements2.put("rdKey", "CircuitID(FullPartial)"); System.out.println( elements2.getString("rdKey")); System.out.println(jsonObj.toString()); } } } } } } } } } } } } } } } } } 

I want you guys to help me if there is a better solution for this. Can I do this without going through the entire json object (until I find the appropriate field)? This solution will not work if the json tree structure receives changes, it should be static as a successful script for this code.

Please suggest the best solution.

+5
source share
2 answers

If you use the flexibility of using the library, you might find JsonPath useful.

You can update all "elements" with "rdKey" using the following code:

 JsonPath.parse(json).set("$..elements[?(@.rdKey)].rdKey", "CircuitID(FullPartial)").json() 
+1
source

If you want to avoid moving JSON, you can use JSONPointer , available in the same org.json library. For instance:.

 String query = <json_pointer_query to element array> JSONPointer pointer = new JSONPointer(query); JSONObject elementsArrayJSON = (JSONObject) pointer.queryFrom(jsonObj); elementsArrayJSON.put("rdKey","CircuitID(FullPartial)"); 

The JSON Pointer request language can be passed to:

https://tools.ietf.org/html/rfc6901

Note: JSON Pointer is pretty simple, it does not support wild card. Thus, you must be sure of the names of the elements, otherwise it will be an exception.

+1
source

Source: https://habr.com/ru/post/1272343/


All Articles