Skip to content
Advertisement

Browse a dynamic json that contains nested lists and dictionaries

I have multi-levels json files with nested list and dictionnary inside, they look like this, but vary in size, and levels:

{
    "mass_update": [
        {
            "lvl-1": "lvl-1.1"
        },
        {
            "lvl-1": "lvl-1.2"
        },
        {
            "lvl-1": "lvl-1.3"
        },
        
        [
            {
                "lvl-2": "lvl-2.1",
                "lvl-2": "lvl-2.1.2"
            },
            [
                {
                    "lvl-3": "lvl-3.1"
                },
                {
                    "lvl-3": "lvl-3.2"
                },
                [
                    {
                        "lvl-4": "lvl-4.1",
                        "lvl-4": "lvl-4.1.2"
                    },
                    {
                        "lvl-4": "lvl-4.2",
                        "lvl-4": "lvl-4.2.2"
                    }
                ]
            ],
            {
                "lvl-2": "lvl-2.2",
                "lvl-2": "lvl-2.2.2"
            },
            [
                {
                    "lvl-3": "lvl-3.3"
                },
                {
                    "lvl-3": "lvl-3.4"
                },
                [
                    {
                        "lvl-4": "lvl-4.3",
                        "lvl-4": "lvl-4.3.2"
                    },
                    {
                        "lvl-4": "lvl-4.4",
                        "lvl-4": "lvl-4.4.2"
                    }
                ]
            ]
        ]
    ]
}

I’m trying to read the json and print every single key value combination. When I’m at the end of a nested list, I would like to execute a function, let’s say for the moment one that do print("This is the end of the list, yay :-) )".

I tried some stuff, like creating a class that has a single var where I stock my dictionnary or my list:

class MassUpt:
def __init__(self, my_dict):
    self.my_dict = my_dict

def get_my_dict(self):
    return self.my_dict

I save the obj from the class inside a global list:

def get_obj(full_dict):      #full_dict contains the list "mass_update" that is in all json files
for item in full_dict:
    if isinstance(item, list):
        obj_dict = MassUpt(item)
        mass_upt_dict.append(obj_dict)
        return get_obj(item)
    else:
        obj_dict = MassUpt(item)
        mass_upt_dict.append(obj_dict)
print()

I then execute another function that is, for the moment, printing the key value combination:

def printing_obj_lst(full_dict):
lst_test = []
for item in full_dict:
    lst_test.append(item.get_my_dict())
for item in lst_test:
    if isinstance(item, list):
        print("Calling next object")
    else:
        for k, v in item.items():
            print("Key: " + str(k) + " Value: " + str(v))

Here is the main that I execute:

full_dict = mass_upt_res_data_json["mass_update"]
    get_obj(full_dict)
    printing_obj_lst(mass_upt_dict)

And here is my output:

Key: lvl-1 Value: lvl-1.1
Key: lvl-1 Value: lvl-1.2
Key: lvl-1 Value: lvl-1.3
Calling next object
Key: lvl-2-1 Value: lvl-2.1.1
Key: lvl-2-2 Value: lvl-2.1.2
Calling next object
Key: lvl-3 Value: lvl-3.1
Key: lvl-3 Value: lvl-3.2
Calling next object
Key: lvl-4-1 Value: lvl-4.1
Key: lvl-4-2 Value: lvl-4.1.2
Key: lvl-4-1 Value: lvl-4.2
Key: lvl-4-2 Value: lvl-4.2.2

I don’t have all the key value combination printed, I don’t know if this is the good solution. It is important that I don’t flatten the json, as the end of list help me know when to execute a futur function. Sorry for the long post, thanks for taking your time to read !

Advertisement

Answer

Easy solution, you really need only a simple recursion if i get it right

full_dict = mass_upt_res_data_json["mass_update"]

def check_inst(elem):
    for e in elem:
        if isinstance(e, list):
            print("list reached")
            check_inst(e)
        else:
            for key, value in e.items() :
                print (key, value)

check_inst(full_dict)

prints:

lvl-1 lvl-1.1
lvl-1 lvl-1.2
lvl-1 lvl-1.3
list reached
lvl-2 lvl-2.1.2
list reached
lvl-3 lvl-3.1
lvl-3 lvl-3.2
list reached
lvl-4 lvl-4.1.2
lvl-4 lvl-4.2.2
lvl-2 lvl-2.2.2
list reached
lvl-3 lvl-3.3
lvl-3 lvl-3.4
list reached
lvl-4 lvl-4.3.2
lvl-4 lvl-4.4.2
Advertisement