Looping on a list in json

Sayth Renshaw flebber.crue at gmail.com
Sat Nov 4 17:01:48 EDT 2017


Hi

I want to get a result from a largish json api. One section of the json structure returns lists of data. I am wanting to get each resulting list returned.

This is my code.
import json
from pprint import pprint

with open(r'/home/sayth/Projects/results/Canterbury_2017-01-20.json', 'rb') as f, open('socks3.json','w') as outfile:
    to_read = json.load(f)


    print(to_read.keys())
    # pprint(to_read)
    meet = to_read["RaceDay"]["Meetings"]
    meeting_id = to_read["RaceDay"]["Meetings"][0]
    pprint(meeting_id.keys())
    # result = meeting_id["Races"][1]["RacingFormGuide"]["Event"]["Runners"]
    result = meeting_id["Races"]
    #failing
    for item in result:
        pprint(["RacingFormGuide"]["Event"]["Runners"])


The key to the issue is that
result = meeting_id["Races"][0]["RacingFormGuide"]["Event"]["Runners"]
result = meeting_id["Races"][1]["RacingFormGuide"]["Event"]["Runners"]
result = meeting_id["Races"][2]["RacingFormGuide"]["Event"]["Runners"]

the numbers though in the above results could go from 0 to 10.

What is the best way to and return the data?
 would just save meeting_id["Races"] to my result however there are a lot of other junk dictionaries and lists I am filtering.

Cheers

Sayth



More information about the Python-list mailing list