Different result for String and Integers using JSON in python
<p><strong>EDIT:</strong>
As @Alfe suggested in the comments, the exact problem in this case is that the following code is unable to handle nodes with same values. So, How do I get the expected output, without changing the value of the nodes?</p>

<p>I'm executing following code to make a tree from JSON data:</p>

<pre><code>from __future__ import print_function
import json
import sys

# Tree in JSON format
s = '{"Harry": {"children": ["Bill", {"Jane": {"children": [{"Diane": {"children": ["Mary"]}}, "Mark"]}}]}}'

# Convert JSON tree to a Python dict
data = json.loads(s)

# Extract tree edges from the dict
edges = []

def get_edges(treedict, parent=None):
    name = next(iter(treedict.keys()))
    if parent is not None:
        edges.append((parent, name))
    for item in treedict[name]["children"]:
        if isinstance(item, dict):
            get_edges(item, parent=name)
        else:
            edges.append((name, item))

get_edges(data)

# Dump edge list in Graphviz DOT format
print('strict digraph tree {')
for row in edges:
    print('    {0} -&gt; {1};'.format(*row))
print('}')
</code></pre>

<p>Command used at terminal: <code>python filename.py | dot -Tpng -otree.png</code></p>

<p>With String input, as shown in the code above, the output is:
<a href="https://i.stack.imgur.com/zFvic.png" rel="nofollow"><img src="https://i.stack.imgur.com/zFvic.png" alt="enter image description here"></a></p>

<p>But if I enter JSON data with integers:</p>

<p><code>s = '{"92": {"children": [{"87": {"children": [87, 96]}}, {"96": {"children": [90, 105]}}]}}'</code></p>

<p>I get following output: (which is wrong!)</p>

<p><a href="https://i.stack.imgur.com/aTnbF.png" rel="nofollow"><img src="https://i.stack.imgur.com/aTnbF.png" alt="enter image description here"></a></p>

<p>Expected output:
<a href="https://i.stack.imgur.com/MVkyL.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/MVkyL.jpg" alt="enter image description here"></a></p>

<p>What am I doing wrong here? How to solve this issue?</p>

<p>That is actually how your data is:</p>

<pre><code>&gt;&gt;&gt; s = '{"92": {"children": [{"87": {"children": [87, 96]}}, {"96": {"children": [90, 105]}}]}}'
&gt;&gt;&gt; print(json.dumps(json.loads(s), indent=2))
{
  "92": {
    "children": [
      {
        "87": {
          "children": [
            87, 
            96
          ]
        }
      }, 
      {
        "96": {
          "children": [
            90, 
            105
          ]
        }
      }
    ]
  }
}
</code></pre>

<p><code>87</code> is listed as a child of <code>87</code>.</p>

python json tree