title: Python - extracting data from JSON response (using TomTom api)
body: <p>I'm trying to retrieve information on route times using the <a href="https://developer.tomtom.com/online-routing/online-routing-documentation-routing/calculate-route" rel="nofollow noreferrer">TomTom API</a>.</p>
  
  <p>By providing the API url, an API key (<a href="https://developer.tomtom.com/user/register" rel="nofollow noreferrer">that you can get by registering</a>) and a source and destination latitude/longitude I want to return the travel time.</p>
  
  <p>I thought that I was getting a dictionary of dictionaries from the response, and should be able to access the data using - jsonTomTomString['routes']['summary']['travelTimeInSeconds’]</p>
  
  <p>but I'm getting ...</p>
  
  <blockquote>
    <p>TypeError: list indices must be integers, not str</p>
  </blockquote>
  
  <p>My code is as follows:</p>
  
  <pre><code>#!/usr/bin/python
  # -*- coding: utf-8 -*-
  
  import ConfigParser
  import requests
  import json
  import sys
  import time
  import datetime
  
  from urllib import urlopen
  
  # URL to the tomtom api
  apiURL      = "https://api.tomtom.com/routing/1/calculateRoute/"
  # apiKey
  apiKey      = "get this from link in description"
  
  #[coordinates]
  sourceLat   = 51.5560241
  sourceLon   = -0.2817075
  destLat     = 53.4630621
  destLon     = -2.2935288
  
  tomtomURL = "%s/%s,%s:%s,%s/json?key=%s" % (apiURL,sourceLat,sourceLon,destLat,destLon,apiKey)
  
  getData = urlopen(tomtomURL).read()
  jsonTomTomString = json.loads(getData)
  
  totalTime = jsonTomTomString['routes']['summary']['totalTimeSeconds']
  
  print ("time to destination is: ", totalTime)
  </code></pre>
  
  <p>The JSON response looks like this...</p>
  
  <blockquote>
    <p>{"formatVersion":"0.0.12","copyright":"Copyright 2018 TomTom
    International BV. All rights reserved. This navigation data is the
    proprietary copyright of TomTom International BV and may be used only
    in accordance with the terms of a fully executed license agreement
    entered into between TomTom International BV, or an authorised
    reseller and yourself. If you have not entered into such a license
    agreement you are not authorised to use this data in any manner and
    should immediately return it to TomTom International
    BV.","privacy":"TomTom keeps information that tells us how and when
    you use our services. This includes information about the device you
    are using and the information we receive while you use the service,
    such as locations, routes, destinations and search queries. TomTom is
    unable to identify you based on the information it collects, and will
    not try to. TomTom uses the information for technical diagnostics, to
    detect fraud and abuse, to create usage reports, and to improve its
    services. The information is kept only for these purposes and for a
    limited period of time, after which it is destroyed. TomTom applies
    security methods based on industry standards to protect the
    information against unauthorised access. TomTom will not give anyone
    else access to the information or use it for any other purpose, unless
    explicitly and lawfully ordered to do so following due legal process.
    You can find out more at <a href="http://tomtom.com/privacy" rel="nofollow noreferrer">http://tomtom.com/privacy</a>. You can contact
    TomTom by going to
    <a href="http://tomtom.com/support" rel="nofollow noreferrer">http://tomtom.com/support</a>.","routes":[{"summary":{"lengthInMeters":326856,"travelTimeInSeconds":13018,"trafficDelayInSeconds":818,"departureTime":"2018-01-08T17:10:31Z","arrivalTime":"2018-01-08T20:47:28Z"},"legs":[{"summary":{"lengthInMeters":326856,"travelTimeInSeconds":13018,"trafficDelayInSeconds":818,"departureTime":"2018-01-08T17:10:31Z","arrivalTime":"2018-01-08T20:47:28Z"},"points":[{"latitude":51.55598,"longitude":-0.28216},{"latitude":51.55601,"longitude":-0.28292},{"latitude":51.55602,"longitude":-0.28384},{"latitude":51.55602,"longitude":-0.28395},{"latitude":51.55602,"longitude":-0.28442},{"latitude":51.55603,"longitude":-0.28493},{"latitude":51.55603,"longitude":-0.28564},{"latitude":51.55603,"longitude":-0.28611},</p>
  </blockquote>
  
  <p>Grateful for help with getting this to work.</p>