title: Parsing a json tree for immediate children in python
body: <p>I have a json tree of the form:</p>
  
  <pre><code>{"id":461000008221732864, "reply":0, "children":[{"id":461000467841945602, "reply":1, "children":[{"id":461006562006298624, "reply":1, "children":[{"id":461007094007595008, "reply":1, "children":[{"id":461011959173771264, "reply":1, "children":[{"id":461012787896926209, "reply":1, "children":[{"id":461014469586329600, "reply":1, "children":[{"id":461014847279210496, "reply":1, "children":[]}]}]}]}]}]},{"id":461000724520787969, "reply":1, "children":[]}]}]}
  </code></pre>
  
  <p>I have to keep a count of reply = 0 or reply = 1 for every node amongst its immediate children. i.e For every node I have to check its immediate children for reply =0/1. I have written a code for finding the number of nodes in a tree. But not able to figure out where should I keep a count for immediate children to check for reply flag. Please help me.</p>
  
  <pre><code>import json
  
  # method to recursively browse the elements
  def get_children(node):
      stack = [node]
      while stack:
          node = stack.pop()
          stack.extend(node['children'][::-1])
          yield node
  
  # open the file and parse its JSON contents
  f = open('filename.json','r')
  rep_dic = {}
  size_dic = {}
  for line in f:
      d = json.loads(line)
      child_dic = {}
      rep = 0
      for child in get_children(d):
          if child not in child_dic:
              child_dic[child["id"]] = 1
              if child["reply"] == 1:
                  rep += 1
  </code></pre>