0

I have the following code to generate json representation of list of lists.

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):
    return [{'name': k, 'children': convert(v) if v else [{}]} for k, v in d.items()]



# Print results
import json
print(json.dumps(convert(root),  indent=4))

Output:

[
"name": "L1",
      "children": [
        {
          "name": "L1",
           "children":[
              {
                "name":"L3",
                "children":[{}]
              },
              {
                "name":"L1",
                "children":[{}]
              }]
        },
        {
            "name":"L2",
            "children":[{}]
        }

      ]

for the levels

Levels=[['L1','L1','L2'],
        ['L1','L1','L3'],
        ['L1','L2'],

I also need to encode the count of each level

for eg there is the path from L1 which has two first level childrens L1(2) and L2(1) followed by L2(1) and L3(1) for next level .

L1(3)-->L1(2)-->L2(1)
             -->L3(1)
     -->L2(1)

How can I encode this count in my json output.

I want my final output to look like this

"name": "L1(3)",
      "children": [
        {
          "name": "L1(2)",
           "children":[
2
  • You want to add values that can be extracted from the data already in a format that is neither intuitive nor machine readable in a simple way? Commented Nov 16, 2018 at 5:07
  • By "count", do you mean the number of leaf nodes or the height of a node? Commented Nov 16, 2018 at 6:48

1 Answer 1

2
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))

OUTPUT

[
  {
    "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": [
              {}
            ]
          }
        ]
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.