0

I need to extract object from the given json based on the node chain passed by user and neglect those which are not in user input, then create a new json object

my master json is :

{
    "menustructure": 
    [
            {
             "node":"Admin",
             "path":"admin",
                "child":[
                        {
                            "node": "Admin.resouce1",
                            "path":"resouce1",
                            "rank":1
                         },
                    
                        {"node":"Admin.resouce2",
                            "path": "oath",
                            "rank":2
                        }
                       ]
            },
            {
                "node":"Workspace",
                "path": "wsp",
                "child":[{
                        "node": "Workspace.system1",
                        "path":"sys1"
                    
                    },
                    {
                        "node": "Workspace.system2",
                        "path":"sys2"
                    }
                ]
            }

        
    ]
}

for example if user pass ['Admin.resource1', 'Workspace'] so expeceted ouput json will be Note '.' in element of user inputted list means that node have child nodes and new json will be having all those child node details including parent node details.

{
    "menustructure": 
    [
            {
             "node":"Admin",
             "path":"admin",
                "child":[
                        {
                            "node": "Admin.resouce1",
                            "path":"resouce1",
                            "rank":1
                         }
                       ]
            },
            {
                "node":"Workspace",
                "path": "wsp",
                "child":[{
                        "node": "Workspace.system1",
                        "path":"sys1"
                    
                    },
                    {
                        "node": "Workspace.system2",
                        "path":"sys2"
                    }
                ]
            }

        
    ]
}

or another example is : ['Admin.resouce2', 'workspace.system1'] then expected json will be:

{
    "menustructure": 
    [
            {
             "node":"Admin",
             "path":"admin",
                "child":[
                        
                        {"node":"Admin.resouce2",
                            "path": "oath",
                            "rank":2
                        }
                       ]
            },
            {
                "node":"Workspace",
                "path": "wsp",
                "child":[{
                        "node": "Workspace.system1",
                        "path":"sys1"
                    
                    }
                ]
            }
    ]
}

or if only single node passed ['Admin'] then output json will be:

{
    "menustructure": 
    [
            {
             "node":"Admin",
             "path":"admin",
                "child":[
                        {
                            "node": "Admin.resouce1",
                            "path":"resouce1",
                            "rank":1
                         },
                    
                        {"node":"Admin.resouce2",
                            "path": "oath",
                            "rank":2
                        }
                       ]
            }   
    ]
}

Any Help will be great.

Code I tried is:

master = json.loads(m)
menustruct = []
nde = ['Admin.resouce1', 'Workspace']
test_master = master['menustructure']
temp_json = test_master
print()

for n in nde:
    temp_data = None
    if "." in n:
        menu_series = n.split(".")
    for m in temp_json:
        temp_data = m
        if temp_data['node'] == menu_series[0]:
            for sub_child in temp_data['child']:
                if sub_child['id'] == menu_series[1]:
                    idx = temp_data['child'].index(sub_child)
                else:
                    temp_data['child'].remove(sub_child)
                #if menu_iter.has_next()
        menustruct.append(temp_data)

print(menustruct)

but its not working as intended problem that I'm facing with this code is if user pass ['Admin.resouce1', 'Admin.resouce2', 'Workspace'] then no data is appended in Admin child and Workspace is getting loaded twice

[{'node': 'Admin', 'path': 'admin', 'child': []}, {'node': 'Workspace', 'path': 'wsp', 'child': [{'node': 'Workspace.system1', 'path': 'sys1'}, {'node': 'Workspace.system2', 'path': 'sys2'}]}, {'node': 'Admin', 'path': 'admin', 'child': []}, {'node': 'Workspace', 'path': 'wsp', 'child': [{'node': 'Workspace.system1', 'path': 'sys1'}, {'node': 'Workspace.system2', 'path': 'sys2'}]}, {'node': 'Admin', 'path': 'admin', 'child': []}, {'node': 'Workspace', 'path': 'wsp', 'child': [{'node': 'Workspace.system1', 'path': 'sys1'}, {'node': 'Workspace.system2', 'path': 'sys2'}]}]

And also this code is not ideal if there are sub-child of child.

vineet singh
  • 173
  • 2
  • 12

1 Answers1

0

Glom addresses exactly this problem.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • I went through the documentation of Glom as I understand Glom will work if I have key node to all dictionaries for example in my case 'Admin': { "node":"Admin", "path":"admin", "child":[ 'resouce0':{ "id": "resouce1", "node": "Admin.resouce1", "path":"resouce0", "rank":0 }, which is not a case in my json , please suggest If I misunderstand something – vineet singh Nov 17 '22 at 07:07