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:

JavaScript

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:

JavaScript

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

JavaScript

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

JavaScript

Here is the main that I execute:

JavaScript

And here is my output:

JavaScript

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

JavaScript

prints:

JavaScript
Advertisement