How to parse different parts of a JSON file in Python?
<p>I'm making a program in Python that gets bus times from a server and prints them. The JSON file looks like this:</p>

<pre><code>{
"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"
},
</code></pre>

<p>I have no problem returning the first block e.g <code>numberofresults</code> by using  </p>

<pre><code> info = json.load(req)
 print info["numberofresults"]
</code></pre>

<p>However, when I try the same thing with <code>duetime</code> it returns:</p>

<pre><code>File "bus.py", line 10, in &lt;module&gt;
    print info["route"]
KeyError: 'route'
</code></pre>

<p>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?</p>

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

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

<pre><code>print info['results']['routes']
</code></pre>

<p>Try above.</p>

python json parsing python-requests