Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve and update json array element without traversing entire json

Tags:

java

json

arrays

I have a very complex json structure. It contains many array elements and those array elements contains other array elements and so on.. Please see below json tree structure.

Json Tree Structure-1 :

enter image description here

Json Tree Structure-2 :

enter image description here

As highlighted above in yellow, I want to update the value of "rdKey" field. I wrote below code and it is perfectly working fine :

String json = "escaped string (as it's 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 any better solution for this. Can I do it without traversing the entire json object (till I find the concerned field) ? This solution will not work if json tree structure gets changes, it needs to be static as a success scenario of this code.

Please suggest better solution.

like image 813
Punit Avatar asked Oct 30 '22 01:10

Punit


2 Answers

If you're flexible on what library to use, maybe the JsonPath will be useful for you.

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

JsonPath.parse(json).set("$..elements[?(@.rdKey)].rdKey", "CircuitID(FullPartial)").json()
like image 116
sjabra Avatar answered Nov 09 '22 12:11

sjabra


If you want to escape traversing of JSON then you can use JSONPointer, available in same org.json library. E.g.:

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

JSON Pointer query language can be referred in:

https://www.rfc-editor.org/rfc/rfc6901

Note: JSON Pointer is pretty basic, it doesn't support wild card. So you need to be sure about element names, otherwise it would throw exception.

like image 30
Sachin Gupta Avatar answered Nov 09 '22 12:11

Sachin Gupta