title: How to access parent of nested json object
body: <p>I have an arbitrary nested JSON object (parsed using json.load), consisting of dicts, lists, primitives etc. I traverse it depth first using recursion and tracking the path to the node in a linux fs format (for lists, I append /element to the path, so there can be multiple objects at a specific path), as following:</p>
  
  <pre><code>def traverse(node, path =''):
     if isinstance(node, dict):
        for key in node:
           traverse(node[key], path+'/'+key)
     elif isinstance(node, list):
        for elem in node:
           traverse(elem,path+'/element')
  </code></pre>
  
  <p>Each node may include a string which needs to be filled in using values of objects that can exist wherever in the tree, referenced in a relative path from the current node, eg: "{../../key} {./child1/child2/key}".<br>
  <strong>Problem</strong>: I can access the values of nodes that are children of the current node but I can't directly access the parent of the current node.<br>
  <strong>Solutions I thought:</strong> One solution I thought is to have a list of tuples (child,parent) and store the current node along with the child I'm about to recurse in, and then search that list in reverse when I need to go up. This is a bit dangerous because if the child is a primitive value then is will be equal to any other children of the same value and type and hence I may retrieve a wrong parent, but I think going through the list in reverse should take care of that right?<br>
  A different solution I thought is to have a dictionary with key being the child's path and value the parent node. I think this should work better because the only time paths clash is with list elements but they all have the same parent so it should be ok I think.  </p>
  
  <p>Are there any other suggestions? Or any comments on these two solutions?
  Thank you</p>