Skip to content
Advertisement

Navigate dict based on its structure

I have a python code that interacts with multiple APIs. All of the APIs return some json but each has different structure. Let’s say I’m looking for people’s names in all these jsons:

JavaScript

As you can see above the dictionaries from jsons have arbitrary structures. I’d like to define something that will serve as a “blueprint” for navigating each json, something like this:

JavaScript

So that if I’m working with json_a I’ll just look into all_jsons["json_a"]["json_structure"] and I have an information on how to navigate this exact json. What would be the best way to achieve this?

Advertisement

Answer

Why not define concrete retrieval functions for each api:

JavaScript

and store them for each endpoint:

JavaScript

I have found this approach more workable than trying to express code-logic by configuration. Then you can easily collect names:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement