0

I have following code that generates parent child relationship in 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)

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

The output generated is in the following format

{
    "L1": {
        "L1": {
            "L2": {},
            "L3": {}
        },
        "L2": {}
    },
    "L2": {
        "L2": {
            "L3": {},
            "L1": {}
        }
    },
    "L3": {
        "L2": {}
    },
    "L4": {
        "L2": {
            "L1": {},
            "L4": {}
        }
    }
}

I want to convert this into the following format which is required for my jquery visualization.

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

      ]

and so on

1 Answer 1

3

You can do this recursively (after you've built root in the format you posted in your question):

def convert(d):
    return [{'name': k, 'children': convert(v) if v else [{}]} for k, v in d.items()]

print(json.dumps(convert(root),  indent=2))

Output

[
  {
    "children": [
      {
        "children": [
          {
            "children": [
              {}
            ],
            "name": "L3"
          },
          {
            "children": [
              {}
            ],
            "name": "L1"
          }
        ],
        "name": "L2"
      }
    ],
    "name": "L2"
  },
  {
    "children": [
      {
        "children": [
          {}
        ],
        "name": "L2"
      }
    ],
    "name": "L3"
  },
  {
    "children": [
      {
        "children": [
          {
            "children": [
              {}
            ],
            "name": "L4"
          },
          {
            "children": [
              {}
            ],
            "name": "L1"
          }
        ],
        "name": "L2"
      }
    ],
    "name": "L4"
  },
  {
    "children": [
      {
        "children": [
          {}
        ],
        "name": "L2"
      },
      {
        "children": [
          {
            "children": [
              {}
            ],
            "name": "L2"
          },
          {
            "children": [
              {}
            ],
            "name": "L3"
          }
        ],
        "name": "L1"
      }
    ],
    "name": "L1"
  }
]
Sign up to request clarification or add additional context in comments.

3 Comments

This returns [] instead of [{}] when d is empty. The simple fix is to just replace convert(v) with convert(v) if len(v) != 0 else [{}]
@GreenCloakGuy Good catch. Thank you
@slider Thank you for the response I had a follow up question. can you please take a look at stackoverflow.com/questions/53331487/…

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.