0

I have a JSON tree of the form

{  
    "id":442500000904671234,
    "reply":0,
    "children":[  
        {  
            "id":442500532536893440,
            "reply":1,
            "children":[  
                {  
                    "id":442500826561785856,
                    "reply":1,
                    "children":[  
                        {  
                            "id":442501277688545280,
                            "reply":1,
                            "children":[  
                                {  
                                    "id":442501561940709376,
                                    "reply":1,
                                    "children":[  
                                        {  
                                            "id":442501884709199872,
                                            "reply":1,
                                            "children":[  

                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {  
            "id":442500099315597312,
            "reply":0,
            "children":[  

            ]
        }
    ]
}

Now, I want to access all the childrens of the tree whose root node is 442500000904671234 from root to the leaf.

With the following python code, I am able to print the first level children

import json
f=open('tree.txt','r')
for line in f:
    d=json.loads(line)
    print len(d["children"])
    for i in range (len(d["children"])):
        print d["children"][i]["id"]

How to I get the all children of the tree?

Joe
  • 46,419
  • 33
  • 155
  • 245
Saurabh
  • 913
  • 3
  • 12
  • 15
  • possible duplicate of [Get the elements from nested JSON with Python using json lib](http://stackoverflow.com/questions/18830955/get-the-elements-from-nested-json-with-python-using-json-lib) – will Mar 10 '15 at 11:39
  • 2
    Is that the correct way to load json? Other very similar questions load the json by doing `json.load(open("tree.txt"))` and then operate on the result of that. You are creating a new `json` object (`d`) for each line of the file. – will Mar 10 '15 at 11:41

1 Answers1

2

In order to retrieve all the children, you might need recursion:

import json

# method to recursively browse the elements
def get_children(node):
    for child in node['children']:
        yield child
        for grandchild in get_children(child):
            yield grandchild

# open the file and parse its JSON contents
f = open('tree.txt','r')
d = json.load(f)
f.close()

# display all the children found
for child in get_children(d):
    print child

For more information, please read:

Community
  • 1
  • 1
Mathieu Rodic
  • 6,637
  • 2
  • 43
  • 49
  • File "/usr/lib/python2.7/json/__init__.py", line 338, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 381, in raw_decode obj, end = self.scan_once(s, idx) RuntimeError: maximum recursion depth exceeded while calling a Python object – Saurabh Mar 13 '15 at 06:48
  • Oh sorry, I was using Python 3.4 – Mathieu Rodic Mar 13 '15 at 09:53