Home > Software engineering >  Converting parent & child dictionary list to nested dictionary
Converting parent & child dictionary list to nested dictionary

Time:06-04

I tried and saw multiple solutions for the above question but could not find a solution that suits my case. I have following list of dictionaries.

[
        {"id": "101", "logical_section": "ORGANIZATION", "parent_section_id": None},
        {"id": "102", "logical_section": "ORG_NAMES", "parent_section_id": "101"},
        {"id": "103", "logical_section": "ORG_ADDRESSES", "parent_section_id": "101"},
        {"id": "104", "logical_section": "SOURCEADDRESS", "parent_section_id": "103"},
        {"id": "105", "logical_section": "STANDARDIZEDADDRESS", "parent_section_id": "103"},
        {"id": "106", "logical_section": "ORG_EMPLOYES", "parent_section_id": "101"}
]

and I need to convert this list to list of nested dictionaries in the following way

{
    "id": "101",
    "logical_section": "ORGANIZATION",
    "parent_section_id": None,
    "child": [
        {
            "id": "102",
            "logical_section": "ORG_NAMES",
            "parent_section_id": "101"
        },
        {
            "id": "103",
            "logical_section": "ORG_ADDRESSES",
            "parent_section_id": "101",
            "child": [
                {"id": "104", "logical_section": "SOURCEADDRESS", "parent_section_id": "103"},
                {"id": "105", "logical_section": "STANDARDIZEDADDRESS", "parent_section_id": "103"}
            ]
        },
        {
            "id": "106",
            "logical_section": "ORG_EMPLOYES",
            "parent_section_id": "101"}
    ]
}

Solutions which I tried are as follow

levels = dict()
for n in test:
    levels.setdefault(n['parent_section_id'], []).append(n)

but this is not giving the output I am looking for.

Any help would be appreciated.

CodePudding user response:

I took a look at this and came up with the following code. I am absolutely certain that this could be done better: I don't recommend using this for large volumes of data. If another, more experienced programmer could weigh in on this solution, I'd be grateful.

test = [
    {"id": "101", "logical_section": "ORGANIZATION", "parent_section_id": None},
    {"id": "102", "logical_section": "ORG_NAMES", "parent_section_id": "101"},
    {"id": "103", "logical_section": "ORG_ADDRESSES", "parent_section_id": "101"},
    {"id": "104", "logical_section": "SOURCEADDRESS", "parent_section_id": "103"},
    {"id": "105", "logical_section": "STANDARDIZEDADDRESS", "parent_section_id": "103"},
    {"id": "106", "logical_section": "ORG_EMPLOYES", "parent_section_id": "101"}
]

ans_dict = test[0]


def insert(tree, node):
    if node["parent_section_id"] == tree["id"]:
        if "child" not in tree:
            tree["child"] = [node]
        else:
            tree["child"].append(node)
    elif "child" in tree:
        for c in tree["child"]:
            insert(c, node)


for record in test:
    insert(ans_dict, record)


print(ans_dict)

This code uses a recursive binary tree approach. What makes it so inefficient is the fact that EVERY SINGLE NODE has the insert() function ran with the given node as a parameter, EVERY SINGLE TIME that a new node is inserted.

  • Related