skip to Main Content

I have json

[
    {
        "id": 1116,
        "en_name": "Location, Surroundings",
        "children": [
            {
                "id": 819,
                "en_name": "Location Satisfaction",
                "children": []
            },
            {
                "id": 1004,
                "en_name": "Car Parking Facilities",
                "children": [
                    {
                        "id": 1297,
                        "en_name": "Availability and general perception of parking",
                        "children": []
                    }
                ]
            },
            {
                "id": 1123,
                "en_name": "On the road ",
                "children": []
            }
        ]
    }
]

I need to find the id and get the name.
For example, if I ask the algorithm to find a name by ID 1297, then it should give
me "Accessibility and general perception of parking".

The algorithm should run through the entire array of the dictionary and also run through all "children", which may also contain "children"

while True:
            for data in self.main_taxonomy:
                while len(data["children"]) != 0:
                    if data["id"] == id:
                        return data["name"]
                    else:
                        data = data["children"]

I tried to do it this way, but it didn’t check all the nesting.

2

Answers


  1. Define a recursive function like this:

    def recursive_find(id_, children):
        for child in children:
            if child.get('id')==id_:
                return child['en_name']
            res = recursive_find(id,child.get('children',[])
            if res:
                return res
        return None
    

    This function will return a en_name property if id matches else returns None, call it on main array of your json like recursive_find(<your_id_here>, data)

    Login or Signup to reply.
  2. This can be solved as a recursive function:

    • Loop over the entries.
    • Your base case, check if the entry has the ID you are looking for
    • Else, if the entry has children, check if any of them have the ID
    • Else, return None.
    def find_element_by_id(entries, ID):
        for entry in entries:
            if entry["id"] == ID:
                return entry["en_name"]
            if entry["children"]:
                rtn = find_element_by_id(entry["children"], ID)
                if rtn:
                    return rtn
        return None
    

    Example use:

    data = [
        {
            "id": 1116,
            "en_name": "Location, Surroundings",
            "children": [
                {
                    "id": 819,
                    "en_name": "Location Satisfaction",
                    "children": []
                },
                {
                    "id": 1004,
                    "en_name": "Car Parking Facilities",
                    "children": [
                        {
                            "id": 1297,
                            "en_name": "Availability and general perception of parking",
                            "children": []
                        }
                    ]
                },
                {
                    "id": 1123,
                    "en_name": "On the road ",
                    "children": []
                }
            ]
        }
    ]
    
    ret1 = find_element_by_id(data, 1297)
    print(ret1)
    

    Output:

    Availability and general perception of parking
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search