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>Editting based on the <em>edit</em> in the question:</p>

<p>As far as your output is considered, it is giving me:</p>

<pre><code>92 -&gt; 87;
87 -&gt; 87;
87 -&gt; 96;
92 -&gt; 96;
96 -&gt; 90;
96 -&gt; 105;
</code></pre>

<p>It is showing <code>"87"</code> and <code>87</code> as same because you are using <code>.format()</code> with <code>print</code> which inserts the value in string independent of maintaing the quotes <code>"</code>. For example:</p>

<pre><code>&gt;&gt;&gt; '{}'.format(1)
'1'
&gt;&gt;&gt; '{}'.format('1')
'1'
</code></pre>

<p>In order to fix this, you may do:</p>

<pre><code>for parent, child in edges:
    parent = '"{}"'.format(parent)  if isinstance(parent, str) else parent
    child = '"{}"'.format(child)  if isinstance(child, str) else child
    print('    {0} -&gt; {1};'.format(parent, child))
</code></pre>

<p>which prints:</p>

<pre><code>"92" -&gt; "87";
"87" -&gt; 87;
"87" -&gt; 96;
"92" -&gt; "96";
"96" -&gt; 90;
"96" -&gt; 105;
</code></pre>

python json tree