-2

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.

Jomy
  • 87
  • 1
  • 13

1 Answers1

0

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)
user16181
  • 126
  • 4
  • Thanks for the quick response. My bad, I missed it. I will need a dynamic FOR LOOP or any loop. Since I have multiple NETSED Loops for many attributes. The one pasted here is just one Attribute with small level of nested. – Jomy Aug 30 '22 at 08:11