pythonjsonpandascanonicalization

Python to Read Multiple Nested JSON into Canonical form


Sample Data

In this attached picture, I need to fetch the VALUE in each ZIP code. This has to be done using python. In this image, I wanted to fetch the underlined values as below:

Expected Output

Can anyone please confirm? I am pretty new to Python.

One blockage here is, there can be multiple nested levels inside this Address-Zip. In the attachment, we have Zip5, Zip4 inside Zip. Similarly we may have Zip1, Zip2, Zip3 etc. to any levels.


Solution

  • Sample code
    import json

    #load json from file
    with open('path_to_file/person.json', 'r') as f:
      data = json.load(f)
    
    for item in data:
        print(item['Zip']['value']['Zip5']['0']['value'])
    

    You can use recursive function like this:

    def item_generator(json_input, lookup_key):
        if isinstance(json_input, dict):
            for k, v in json_input.items():
                if k == lookup_key:
                    yield v
                else:
                    yield from item_generator(v, lookup_key)
        elif isinstance(json_input, list):
            for item in json_input:
                yield from item_generator(item, lookup_key)