Ryan Werner Ryan Werner - 2 months ago 7
JSON Question

How to parse different parts of a JSON file in Python?

I'm making a program in Python that gets bus times from a server and prints them. The JSON file looks like this:

{
"errorcode": "0",
"errormessage": "",
"numberofresults": 4,
"stopid": "175",
"timestamp": "28/09/2016 10:32:44",
"results": [
{
"arrivaldatetime": "28/09/2016 10:43:36",
"duetime": "10",
"departuredatetime": "28/09/2016 10:43:36",
"departureduetime": "10",
"scheduledarrivaldatetime": "28/09/2016 10:44:00",
"scheduleddeparturedatetime": "28/09/2016 10:44:00",
"destination": "Kimmage",
"destinationlocalized": "Camaigh",
"origin": "Harristown",
"originlocalized": "Baile Anraí",
"direction": "Outbound",
"operator": "bac",
"additionalinformation": "",
"lowfloorstatus": "no",
"route": "83",
"sourcetimestamp": "28/09/2016 09:44:49",
"monitored": "true"
},
{
"arrivaldatetime": "28/09/2016 10:43:56",
"duetime": "11",
"departuredatetime": "28/09/2016 10:43:56",
"departureduetime": "11",
"scheduledarrivaldatetime": "28/09/2016 10:14:00",
"scheduleddeparturedatetime": "28/09/2016 10:14:00",
"destination": "Kimmage",
"destinationlocalized": "Camaigh",
"origin": "Harristown",
"originlocalized": "Baile Anraí",
"direction": "Outbound",
"operator": "bac",
"additionalinformation": "",
"lowfloorstatus": "no",
"route": "83",
"sourcetimestamp": "28/09/2016 10:32:40",
"monitored": "true"
}, {
"errorcode": "0",
"errormessage": "",
"numberofresults": 4,
"stopid": "175",
"timestamp": "28/09/2016 10:32:44",
"results": [
{
"arrivaldatetime": "28/09/2016 10:43:36",
"duetime": "10",
"departuredatetime": "28/09/2016 10:43:36",
"departureduetime": "10",
"scheduledarrivaldatetime": "28/09/2016 10:44:00",
"scheduleddeparturedatetime": "28/09/2016 10:44:00",
"destination": "Kimmage",
"destinationlocalized": "Camaigh",
"origin": "Harristown",
"originlocalized": "Baile Anraí",
"direction": "Outbound",
"operator": "bac",
"additionalinformation": "",
"lowfloorstatus": "no",
"route": "83",
"sourcetimestamp": "28/09/2016 09:44:49",
"monitored": "true"
},
{
"arrivaldatetime": "28/09/2016 10:43:56",
"duetime": "11",
"departuredatetime": "28/09/2016 10:43:56",
"departureduetime": "11",
"scheduledarrivaldatetime": "28/09/2016 10:14:00",
"scheduleddeparturedatetime": "28/09/2016 10:14:00",
"destination": "Kimmage",
"destinationlocalized": "Camaigh",
"origin": "Harristown",
"originlocalized": "Baile Anraí",
"direction": "Outbound",
"operator": "bac",
"additionalinformation": "",
"lowfloorstatus": "no",
"route": "83",
"sourcetimestamp": "28/09/2016 10:32:40",
"monitored": "true"
},


I have no problem returning the first block e.g
numberofresults
by using

info = json.load(req)
print info["numberofresults"]


However, when I try the same thing with
duetime
it returns:

File "bus.py", line 10, in <module>
print info["route"]
KeyError: 'route'


I think this is because the same key turns up multiple times for different buses in the JSON file. How do I specify which bus I want Python to get info for?

File "bus.py", line 10, in
print info['results']['routes']
TypeError: list indices must be integers, not str

EDIT: Managed to get it working by using
print info["results"][0]["route"]
etc. Thanks all.

Answer
print info['results']['routes']

Try above.

Comments