title: Adding nodes to json in python
body: <p>I am trying to generate custom JSON in python using the following code</p>
  
  <pre><code>root={}
  Levels=[['L1','L1','L2'],
          ['L1','L1','L3'],
          ['L1','L2'],
          ['L2','L2','L3'],
          ['L2','L2','L1'],
          ['L3','L2'],
          ['L4','L2','L1'],
          ['L4','L2','L4']]
  
  def append_path(root, paths):
      if paths:
          child = root.setdefault(paths[0], {})
          append_path(child, paths[1:])
  
  for p in Levels:
      append_path(root, p)
  
  def convert(d):
      templist=[]
      noofchildren=0
      if(len(d.items())==0):
          return ([{}],1)
      for k,v in d.items():
          temp,children=convert(v)
          noofchildren+=children
          if(temp):
              templist.append({"name":k+"("+str(children)+")",'children':temp})
          else:
              templist.append({'name': k+"("+str(children)+")", 'children':[{}]})
  
      return (templist,noofchildren)    
  
  # Print results
  import json
  print(json.dumps(convert(root)[0],  indent=2))
  </code></pre>
  
  <p>and the OUTPUT is</p>
  
  <pre><code>[
    {
      "name": "L1(3)",
      "children": [
        {
          "name": "L1(2)",
          "children": [
            {
              "name": "L2(1)",
              "children": [
                {}
              ]
            },
            {
              "name": "L3(1)",
              "children": [
                {}
              ]
            }
          ]
        },
        {
          "name": "L2(1)",
          "children": [
            {}
          ]
        }
      ]
    },
    {
      "name": "L2(2)",
      "children": [
        {
          "name": "L2(2)",
          "children": [
            {
              "name": "L3(1)",
              "children": [
                {}
              ]
            },
            {
              "name": "L1(1)",
              "children": [
                {}
              ]
            }
          ]
        }
      ]
    },
    {
      "name": "L3(1)",
      "children": [
        {
          "name": "L2(1)",
          "children": [
            {}
          ]
        }
      ]
    },
    {
      "name": "L4(2)",
      "children": [
        {
          "name": "L2(2)",
          "children": [
            {
              "name": "L1(1)",
              "children": [
                {}
              ]
            },
            {
              "name": "L4(1)",
              "children": [
                {}
              ]
            }
          ]
        }
      ]
    }
  ]
  </code></pre>
  
  <p>My dataset has changed a little bit</p>
  
  <pre><code> Levels=[[['L1','L1','L2'],[10,20,30]],
          [[['L1','L1','L3'],[10,15,20]],
          [[['L1','L2'],[20,10]],
          [[['L2','L2','L3'],[20,20,30]],
          [[['L2','L2','L1'],[10,20,30]]
          [[['L3','L2'],[10,20]]
          [[['L4','L2','L1'],[10,20,10]]
          [[['L4','L2','L4'],[20,40,50]]]
  </code></pre>
  
  <p>and the output that I want is the average of the levels along with the count</p>
  
  <pre><code>[
    {
      "name": "L1(3)#(13)", // taking avg of 10,10,20
      "children": [
        {
          "name": "L1(2)#(17)", // taking avg of 20,15
          "children": [
            {
              "name": "L2(1)#(30)",
              "children": [
                {}
              ]
            },
            {
              "name": "L3(1)#(20)",
              "children": [
                {}
              ]
            }
          ]
        },
        {
          "name": "L2(1)#10",
          "children": [
            {}
          ]
        }
      ]
    },
    {
      "name": "L2(2)#(15)", // avg of 20,10
      "children": [
        {
          "name": "L2(2)#(20)", // avg of 20,20
          "children": [
            {
              "name": "L3(1)#(30)",
              "children": [
                {}
              ]
            },
            {
              "name": "L1(1)#(30)",
              "children": [
                {}
              ]
            }
          ]
        }
      ]
    },
    {
      "name": "L3(1)#(10)",
      "children": [
        {
          "name": "L2(1)#(10)",
          "children": [
            {}
          ]
        }
      ]
    },
    {
      "name": "L4(2)#(15)",// avg of 10,20
      "children": [
        {
          "name": "L2(2)#(30)", // avg of 20,40
          "children": [
            {
              "name": "L1(1)# (10)",
              "children": [
                {}
              ]
            },
            {
              "name": "L4(1)#(50)",
              "children": [
                {}
              ]
            }
          ]
        }
      ]
    }
  ]
  </code></pre>
  
  <p>How can i change my code to add this information?</p>