B
B
bankinobi2018-04-25 12:21:23
Python
bankinobi, 2018-04-25 12:21:23

How to store paths for json file?

Good afternoon.
It is necessary to parse the json file, extract 130 fields from it.
The working version works without errors.

data_item = json.load(infile)
    print(data_item['data']['variables']['startupProcessInfo']['iin'])

How to organize the "storage" of these 130 paths, so as not to clutter up the script?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Elvis, 2018-04-26
@bankinobi

json = {'data':{'variables':{'startupProcessInfo':{'iin': 'first'}}, 'piid': 'second', 'executionState': 'third', 'creationTime': 'fourth'}}
path_arr = ["['data']['variables']['startupProcessInfo']['iin']",
         "['data']['piid']",
         "['data']['executionState']",
         "['data']['creationTime']"]

for x in path_arr:
    json_iter = json
    path = x[2:-2].split("']['")
    for p in path:
        json_iter = json_iter[p]
    print(json_iter)

Conclusion:
========================== RESTART: E:/Python/1.py ==========================
first
second
third
fourth

As an example. you can change it to your code. here I asked how to pass a string as a path.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question