I have a json object that is very deep. In other words, I have a dictionary containing dictionaries containing dictionaries, etc. Many times. Thus, it can be imagined as a huge tree in which some nodes are very far from the root of the node.
Now I would like to cut this tree so that I have only nodes in it, separated by no more than N steps from the root. Is there an easy way to do this?
For example, if I have:
{'a':{'d':{'e':'f', 'l':'m'}}, 'b':'c', 'w':{'x':{'z':'y'}}}
And I want to save only nodes that are 2 steps from the root, I should get:
{'a':{'d':'o1'}, 'b':'c', 'w':{'x':'o2'}}
So, I just replace the far-standing dictionaries with individual meanings.
Roman source
share