How to merge and create dict of dicts from a dictionary [closed]

I have a dictionary like the one below in which the value of one of the list elements will be a key somewhere in the same dictionary.

{"a": ["b", "c"], "b": ["D"], "c": ["A", "B", "C"], "A": ["abc", "aab", "aba"], "B": ["bcd", "bdc"], "C": ["dab", "dbc", "def", "dgr"], "abc": ["eee", "ehj"], "eee": ["ghi"], "aab": ["tuv", "xuv"], "ehj": ["giu"], "aba": ["suv", "ruv"]}

I want to merge all of them as below.

{"a": [{"b": ["D"]}, {"c": [{"A": [{"abc": [{"eee": ["ghi"], "ehj": ["giu"]}, {"aab": ["tuv", "xuv"]}, {"aba": ["suv", "ruv"]}]}, {"B": ["bcd", "bdc"]}, {"C": ["dab", "dbc", "def", "dgr"]}]}]}]}

JSON Format:

{
    "a": [{
        "b": ["D"]
    }, {
        "c": [{
            "A": [{
                "abc": [{
                    "eee": ["ghi"],
                    "ehj": ["giu"]
                }, {
                    "aab": ["tuv", "xuv"]
                }, {
                    "aba": ["suv", "ruv"]
                }]
            }, {
                "B": ["bcd", "bdc"]
            }, {
                "C": ["dab", "dbc", "def", "dgr"]
            }]
        }]
    }]
}

Also, the number of values (list of key elements) of a key is not equal.

Thanks for your help!



Read more here: https://stackoverflow.com/questions/68456276/how-to-merge-and-create-dict-of-dicts-from-a-dictionary

Content Attribution

This content was originally published by shikhar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: