Search code examples
pythonjsonapigoogle-mapskeyerror

Whenever I try parsing JSON file I get KeyError in Python


I am using Google Maps API Here is my code:

import googlemaps
import urllib2
import json

apiKey = '****************************'
url = 'https://maps.googleapis.com/maps/api/directions/json?departure_time=now&origin='
origin = raw_input("Origin ")
destination = raw_input("Destination ")
finalUrl = url + origin + '&destination=' + destination + '&key=' + apiKey

json_obj = urllib2.urlopen(finalUrl)
data = json.load(json_obj)

for item in data['routes']:
    print item['duration_in_traffic']

This is the error I get:

Traceback (most recent call last):

File "C:/Users/Documents/python/map2.py", line 15, in

print item['duration_in_traffic']

KeyError: 'duration_in_traffic'

I have gone through the JSON file and that key is definitely there.

Here is the item

{u'routes': [{u'overview_polyline': {u'points': u'cxuyH_|Mf@bB~BsB?A?A?C@IYyAOw@EAGKCQ@I@A_@[OYU_@]eAoAeEaBsHk@gCu@uC_@oAWk@Y]SM_Aa@iAe@_F}@oDk@q@GgAAoBHwAHiCPoAHsA@o@@aDd@g@LkAHs@AgC[s@CoA[kAKEqBKsAw@gFwBcNoAmJg@_F'}, u'warnings': [], u'bounds': {u'northeast': {u'lat': 51.5752274, u'lng': 0.09127539999999999}, u'southwest': {u'lat': 51.5615695, u'lng': 0.0758157}}, u'waypoint_order': [], u'summary': u'Ley St', u'copyrights': u'Map data \xa92015 Google', u'legs': [{u'distance': {u'text': u'2.3 km', u'value': 2293}, u'end_address': u'Ilford, Ilford, Greater London IG2, UK', u'via_waypoint': [], u'duration_in_traffic': {u'text': u'5 mins', u'value': 315}, u'start_address': u'Ilford, Ilford, Greater London IG1, UK', u'start_location': {u'lat': 51.5624203, u'lng': 0.0763182}, u'steps': [{u'html_instructions': u'Head southwest on Thorold Rd toward Adelaide Rd', u'distance': {u'text': u'42 m', u'value': 42}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 51.5624203, u'lng': 0.0763182}, u'polyline': {u'points': u'cxuyH_|Mb@zABF'}, u'duration': {u'text': u'1 min', u'value': 6}, u'end_location': {u'lat': 51.5622154, u'lng': 0.0758157}}, {u'html_instructions': u'Turn left at Adelaide Rd', u'distance': {u'text': u'82 m', u'value': 82}, u'travel_mode': u'DRIVING', u'maneuver': u'turn-left', u'start_location': {u'lat': 51.5622154, u'lng': 0.0758157}, u'polyline': {u'points': u'{vuyH{xM~BsB'}, u'duration': {u'text': u'1 min', u'value': 33}, u'end_location': {u'lat': 51.56157770000001, u'lng': 0.0763982}}, {u'html_instructions': u'At the roundabout, take the 1st exit onto Hainault St', u'distance': {u'text': u'62 m', u'value': 62}, u'travel_mode': u'DRIVING', u'maneuver': u'roundabout-left', u'start_location': {u'lat': 51.56157770000001, u'lng': 0.0763982}, u'polyline': {u'points': u'{ruyHo|M?A?A?A?A?A?A?A?A@??AU_ACYKw@'}, u'duration': {u'text': u'1 min', u'value': 16}, u'end_location': {u'lat': 51.5617618, u'lng': 0.0772199}}, {u'html_instructions': u'At the roundabout, take the 1st exit onto Ley St', u'distance': {u'text': u'1.5 km', u'value': 1539}, u'travel_mode': u'DRIVING', u'maneuver': u'roundabout-left', u'start_location': {u'lat': 51.5617618, u'lng': 0.0772199}, u'polyline': {u'points': u'tuyHsaNA?A?A?A??AA?AAA??AAA?AA??A?AAA?A?A?A?AAA?A?A?A?A@??A?A?A?A@A@[OYU_@]eAoAeEaBsHYwAQo@?Eu@oCUw@IWIUMUEKSQKIGCq@[MEiAe@F}@KAcDi@q@GgAAsAD[Bi@Bm@D{@HmAFoAHsA@o@@aDd@G@@JcAFG@s@AgC[s@CoA[i@G'}, u'duration': {u'text': u'4 mins', u'value': 263}, u'end_location': {u'lat': 51.5734933, u'lng': 0.08373649999999999}}, {u'html_instructions': u'Turn right onto Eastern Ave/A12Destination will be on the left', u'distance': {u'text': u'0.6 km', u'value': 568}, u'travel_mode': u'DRIVING', u'maneuver': u'turn-right', u'start_location': {u'lat': 51.5734933, u'lng': 0.08373649999999999}, u'polyline': {u'points': u'i}wyHkjOa@CA}@Cs@Eq@Ea@EYYqBSmACMMw@SsA]yBw@}Ey@eGGg@E_@G_@[mCKqA'}, u'duration': {u'text': u'1 min', u'value': 81}, u'end_location': {u'lat': 51.5752274, u'lng': 0.09127539999999999}}], u'duration': {u'text': u'7 mins', u'value': 399}, u'end_location': {u'lat': 51.5752274, u'lng': 0.09127539999999999}}]}], u'status': u'OK', u'geocoded_waypoints': [{u'place_id': u'ChIJA-CogdWl2EcRwTLERk1SnHY', u'geocoder_status': u'OK', u'types': [u'postal_code_prefix', u'postal_code']}, {u'place_id': u'ChIJSdgOJwWk2EcR_xRqH8zm13o', u'geocoder_status': u'OK', u'types': [u'postal_code_prefix', u'postal_code']}]}


Solution

  • item does not contain a duration_in_traffic key.

    For your example output, the correct path to the duration_in_traffic value is item['legs'][0]['duration_in_traffic'].