Parsing JSON file with Python -> google map api

Posted by Hannes on Stack Overflow See other posts from Stack Overflow or by Hannes
Published on 2011-01-09T13:22:56Z Indexed on 2011/01/09 15:53 UTC
Read the original article Hit count: 292

Filed under:
|
|
|
|

Hi all,

I am trying to get started with JSON in Python, but it seems that I misunderstand something in the JSON concept. I followed the google api example, which works fine. But when I change the code to a lower level in the JSON response (as shown below, where I try to get access to the location), I get the following error message for code below:

Traceback (most recent call last):
File "geoCode.py", line 11, in test = json.dumps([s['location'] for s in jsonResponse['results']], indent=3) KeyError: 'location'

How can I get access to lower information level in the JSON file in python? Do I have to go to a higher level and search the result string? That seems very weird to me?

Here is the code I have tried to run:

import urllib, json

URL2 = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"

googleResponse = urllib.urlopen(URL2); jsonResponse = json.loads(googleResponse.read())

test = json.dumps([s['location'] for s in jsonResponse['results']], indent=3)

print test

Thank you for your responses.

© Stack Overflow or respective owner

Related posts about python

Related posts about JSON