Home > Mobile >  Write multiple nested dictionary with different number of layers to a JSON file with specific format
Write multiple nested dictionary with different number of layers to a JSON file with specific format

Time:02-23

I have a list of multiple dictionaries with different numbers of layers. Here's what it looks like:

data_ls = [
    {"a": {"b": {"c1": {"d1": "d1_value"}}}},
    {"a": {"b": {"c2": {"d2": {"e1": "e1_value "}}}}},
    ...
    ...
]

I need to write it to a JSON file, here's what I tried:

json_str = json.dumps(data_ls)
json_file = open("data.json", "w")
json_file.write(json_str)

The output will be like:

[
    {
        "a": {
            "b": {
                "c1": {
                    "d1": "d1_value"
                }
            }
        }
    },
    {
        "a": {
            "b": {
                "c2": {
                    "d2": {
                        "e1": "e1_value "
                    }
                }
            }
        }
    }
]

But some of the same keys are turned out to be separated nested, the desired output looks like:

[{
    "a": {
        "b": {
            "c1": {"d1": "d1_value"},
            "c2": {
                "d2": {"e1": "e1_value "},
            },
        }
    }
}]

How do I get the output like this? Thanks in advance!

CodePudding user response:

You can use a recursive function with two dictionaries at a time, checking whether the key exists or not if not update the key

import json

data = [
    {},
    {'a': {'b': {'c1': {'d1': 'd1_value'}}}},
    {'a': {'b': {'c2': {'d2': {'e1': 'e1_value '}}}}},
    {'a': {'b1': {'c3': 'd3'}}},
    {'x': {'y': 'z'}},
    {'a': {'b': {'c2': {'d2': {'e2': 'e2_value '}}}}}
]

def fun(d1: dict, d2: dict):
    for k, v in d2.items():
        if k not in d1:
            d1[k] = v
        if isinstance(v, dict):
            return fun(d1[k], v)


res = data[0]
for d in data[1:]:
    fun(res, d)

print(json.dumps(res))

Output:

{
    "a": {
        "b": {
            "c1": {
                "d1": "d1_value"
            },
            "c2": {
                "d2": {
                    "e1": "e1_value ",
                    "e2": "e2_value "
                }
            }
        },
        "b1": {
            "c3": "d3"
        }
    },
    "x": {
        "y": "z"
    }
}

Note:

I'm only considering nested elements as dicts and other non sequence types

  • Related