Search code examples
pythonjsond3.jshierarchical-data

Create hierarchical Json from nested dictory in python


I have nested dictionary like below (it is cosmos gremlin output), which I want to convert in to a hierarchical json in python. I want to use this in D3.js for creating a hierarchical tree. Can you pls let me know how to solve this? Appreciate your response.

example nested dictionary:

{"Root_Level":{"key":"Root_Level","value":{
            "Operation":{
               "key":"Operation",
               "value":{}
            },
            "Technology":{
               "key":"Technology",
               "value":{
                  "Top Management":{
                     "key":"Top Management",
                     "value":{
                        "Associate Product Lead":{
                           "key":"Associate Product Lead",
                           "value":{
                              "Associate Architect":{
                                 "key":"Associate Architect",
                                 "value":{
                                    "Principal Consultant":{
                                       "key":"Principal Consultant",
                                       "value":{}
                                    }}}}}}}}}}}}

expected output:

{ "name": "Root_Level", "children":[ { "name": "Operation", "children": [] }, { "name": "Technology", "children":[ { "name": "Top Management", "children": [ { "name": "Associate Product Lead" ,"children": [ { "name": "Associate Architect" ,"children": [ {
"name": "Principal Consultant", "children": [] }]}]}]}]}]}

Solution

  • You can use the function below to get what you want.

    result = tree(data)[0] the [0] is needed because the first element is what you want.

    def tree(data):
        result = []
        if isinstance(data, dict):
            for v in data.values():
                temp = {'name': v['key']}
                temp['children'] = tree(v['value'])
                result.append(temp)
        return result
    
    data = {"Root_Level":{"key":"Root_Level","value":{ "Operation":{ "key":"Operation", "value":{} }, "Technology":{ "key":"Technology", "value":{ "Top Management":{ "key":"Top Management", "value":{ "Associate Product Lead":{ "key":"Associate Product Lead", "value":{ "Associate Architect":{ "key":"Associate Architect", "value":{ "Principal Consultant":{ "key":"Principal Consultant", "value":{} }}}}}}}}}}}}
    result = tree(data)[0]
    
    print(result)
    

    Output

    {'name': 'Root_Level',
     'children': [{'name': 'Operation', 'children': []},
                  {'name': 'Technology',
                   'children': [{'name': 'Top Management',
                                 'children': [{'name': 'Associate Product Lead',
                                               'children': [{'name': 'Associate '
                                                                     'Architect',
                                                             'children': [{'name': 'Principal '
                                                                                   'Consultant',
                                                                           'children': []}]}]}]}]}]}