Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding deeply nested key/value in JSON

Tags:

java

json

Suppose I have a JSON array like this:

[
    {
        "id": "429d30a1-9364-4d9a-92e0-a17e00b3afba",
        "children": [],
        "parentid": "",
        "name": "Expo Demo"
    }, 
    {
        "id": "f80f1034-9110-4349-93d8-a17e00c9c317",
        "children": 
            [
                {
                    "id":"b60f2c1d-368b-42c4-b0b2-a1850073e1fe", 
                    "children":[], 
                    "parentid":"f80f1034-9110-4349-93d8-a17e00c9c317", 
                    "name":"Tank"
                }
            ],
        "parentid": "",
        "name": "Fishtank"
    }, 
    {
        "id": "fc8b0697-9406-4bf0-b79c-a185007380b8",
        "children": [
            {
                "id":"5ac52894-4cb6-46c2-a05a-a18500739193", 
                "children":[
                    {
                        "id": "facb264c-0577-4627-94a1-a1850073c270",
                        "children":[
                            {
                                "id":"720472b5-189e-47f1-97a5-a18500a1b7e9", 
                                "children":[], 
                                "parentid":"facb264c-0577-4627-94a1-a1850073c270", 
                                "name":"ubSubSub"
                            }],
                        "parentid": "5ac52894-4cb6-46c2-a05a-a18500739193",
                        "name": "Sub-Sub1"
                    }], 
                "parentid":"fc8b0697-9406-4bf0-b79c-a185007380b8", "name":"Sub"
            },
            {
                "id":"4d024610-a39b-49ce-8581-a18500739a75", 
                "children":[], 
                "parentid":"fc8b0697-9406-4bf0-b79c-a185007380b8", 
                "name":"Sub2"
            }
        ],
        "parentid": "",
        "name": "Herman"
    }, 
    {
        "id": "a5b140c9-9987-4e6d-a883-a18c00726883",
        "children": [
            {
                "id":"fe103303-fd5e-4cd6-81a0-a18c00733737", 
                "children":[], 
                "parentid":"a5b140c9-9987-4e6d-a883-a18c00726883", 
                "name":"Contains Spaces"
            }],
        "parentid": "",
        "name": "Kiosk"
    }
]

No I want to find a certain object based on a id and once I have that, I need its children and all its childrends children

So lets say i want to find the element with an id if 4d024610-a39b-49ce-8581-a18500739a75

That should find the Element Sub2

And now it should produce all the child elements ids witch will be:

facb264c-0577-4627-94a1-a1850073c270
720472b5-189e-47f1-97a5-a18500a1b7e9

Let say I would do

findElementsChildren("4d024610-a39b-49ce-8581-a18500739a75")

So i guess its two parts, first find the "parent" element. Then find its childrends childrends children etc..

Any help would be much appreciated!

like image 558
Harry Avatar asked Sep 29 '26 08:09

Harry


1 Answers

You can use recursion to solve the problem of unlimited nesting. With Gson, it would be something like the following code snippet (not tested). Other libraries will provide structures as JsonElement as well.

private JsonElement findElementsChildren(JsonElement element, String id) {
    if(element.isJsonObject()) {
        JsonObject jsonObject = element.getAsJsonObject();
        if(id.equals(jsonObject.get("id").getAsString())) {
            return jsonObject.get("children");
        } else {
            return findElementsChildren(element.get("children").getAsJsonArray(), id);
        }
    } else if(element.isJsonArray()) {
        JsonArray jsonArray = element.getAsJsonArray();
        for (JsonElement childElement : jsonArray) {
            JsonElement result = findElementsChildren(childElement, id);
            if(result != null) {
                return result;
            }
        }
    }

    return null;
}
like image 186
Stefan Jansen Avatar answered Sep 30 '26 21:09

Stefan Jansen