-1
body = {
    'a': 1,
    'b': 'apple',
    'child': [
        {
            'a': 12,
            'b': 'banana',
            'child': [
                {
                    'a': 121,
                    'b': 'mango',
                    'child': [
                        {
                            'a': 1211,
                            'b': 'coconut',
                            'child': [dynamic nested]
                        }
                    ]
                },
                {
                    'a': 122,
                    'b': 'papaya',
                    'child': [
                        {
                            'a': 1221,
                            'b': 'lemon',
                            'child': [dynamic nested]
                        }
                    ]
                }
            ]
        },
        {
            'a': 13,
            'b': 'orenge',
            'child': [
                dynamic nested
            ]
        }
    ]
}

if i want know index of 'coconut' or 'lemon' (json body dynamic children sub child i don't know dee child, but khow 'a' or 'b' for find index deep)

how to get index with python?

ex1: index of 'coconut' = [0,0,0]
ex2: index of 'lemon' = [0,1,0]
  • Can you share what you have tried in terms of code to solve this? – Devesh Kumar Singh Jul 30 '21 at 14:25
  • I use this code find 'a' or 'b' but i don't know find index this item [link](https://stackoverflow.com/questions/66200377/finding-all-json-keys-with-a-certain-name-in-python) – Horizon Tree Jul 30 '21 at 14:31
  • Please add the code in the description instead of providing a link – Devesh Kumar Singh Jul 30 '21 at 14:32
  • Does this answer your question? [find the path of the targeted values in a nested python dictionary and list](https://stackoverflow.com/questions/45230554/find-the-path-of-the-targeted-values-in-a-nested-python-dictionary-and-list) or perhaps https://stackoverflow.com/questions/41777880/functions-that-help-to-understand-jsondict-structure/41778581#41778581 – JonSG Jul 30 '21 at 15:39

2 Answers2

2
def find(child, value, index=False):
     # First element in nested object is searched value
     if child["b"] == value:
         return []
     if len(child["child"]) == 0:
         return False
     for i in range(len(child["child"])):
         if child["child"][i]["b"] == value:
             index = [i]
             break
         else:
             index = find(child["child"][i], value, index)
             if index != False:
                  index = [i] + index
                 break
     return index
print(find(body, "coconut"))

Use a recursive function

Akane
  • 56
  • 3
0

For educational purposes, the same Akane's algorithm but in a little bit more Pythonic style:

def find_fruit(child, value, index=False):
    if child["b"] == value: 
        return []
    for i, ch in enumerate(child["child"]):
        if ch["b"] == value: 
            return [i]
        index = find_fruit(ch, value, index)
        if index: 
            return [i] + index

print(find(body, "coconut"))
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23