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?

2
  • possible duplicate of Get the elements from nested JSON with Python using json lib
    – will
    Mar 10, 2015 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, 2015 at 11:41

1 Answer 1

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:

2
  • 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, 2015 at 6:48
  • Oh sorry, I was using Python 3.4 Mar 13, 2015 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.