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:

json_a = {
    "people": [
        {"name": "John"},
        {"name": "Peter"}
    ]
}

json_b = {
    "humans": {
        "names": ["Adam", "Martin"]
    }
}

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:

all_jsons = {
    "json_a": {
        "url": "http://endpoint",
        "json_structure": "people -> list -> name"
    },
    "json_b": {
        "url": "http://someotherendpoint",
        "json_structure": "humans -> names -> list"
    }
}

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:

def retrieve_a(data):
    return [d["name"] for d in data["people"]]

def retrieve_b(data):
    return data["humans"]["names"]

and store them for each endpoint:

all_jsons = {
    "json_a": {
        "url": "http://endpoint",
        "retrieve": retrieve_a
    },
    "json_b": {
        "url": "http://someotherendpoint",
        "retrieve": retrieve_b
    }
}

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

for dct in all_jsons.values():
    data = ...  # requests.get(dct["url"]).json()  # or similar
    names = dct["retrieve"](data)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement