2

I am trying to get started with JSON in Python, but it seems that I misunderstand something in the JSON concept. I followed thegoogle 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
<module>
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, jsonURL2 = "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
John Conde's user avatar
John Conde
220k100 gold badges464 silver badges504 bronze badges
askedJan 9, 2011 at 13:22
Hannes's user avatar

2 Answers2

18

The key to understandingjsonResponse's format is to print it out:

import urllib, jsonimport pprintURL2 = "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())pprint.pprint(jsonResponse)# {u'results': [{u'address_components': [{u'long_name': u'1600',#                                         u'short_name': u'1600',#                                         u'types': [u'street_number']},#                                        {u'long_name': u'Amphitheatre Pkwy',#                                         u'short_name': u'Amphitheatre Pkwy',#                                         u'types': [u'route']},#                                        {u'long_name': u'Mountain View',#                                         u'short_name': u'Mountain View',#                                         u'types': [u'locality',#                                                    u'political']},#                                        {u'long_name': u'San Jose',#                                         u'short_name': u'San Jose',#                                         u'types': [u'administrative_area_level_3',#                                                    u'political']},#                                        {u'long_name': u'Santa Clara',#                                         u'short_name': u'Santa Clara',#                                         u'types': [u'administrative_area_level_2',#                                                    u'political']},#                                        {u'long_name': u'California',#                                         u'short_name': u'CA',#                                         u'types': [u'administrative_area_level_1',#                                                    u'political']},#                                        {u'long_name': u'United States',#                                         u'short_name': u'US',#                                         u'types': [u'country',#                                                    u'political']},#                                        {u'long_name': u'94043',#                                         u'short_name': u'94043',#                                         u'types': [u'postal_code']}],#                u'formatted_address': u'1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA',#                u'geometry': {u'location': {u'lat': 37.4216227,#                                            u'lng': -122.0840263},#                              u'location_type': u'ROOFTOP',#                              u'viewport': {u'northeast': {u'lat': 37.424770299999999,#                                                           u'lng': -122.0808787},#                                            u'southwest': {u'lat': 37.418475100000002,#                                                           u'lng': -122.0871739}}},#                u'types': [u'street_address']}],#  u'status': u'OK'}test = json.dumps([s['geometry']['location'] for s in jsonResponse['results']], indent=3)print(test)# [#    {#       "lat": 37.4216227, #       "lng": -122.0840263#    }# ]
  1. jsonResponse is a dict.
  2. jsonResponse['results'] is a list of dicts.
  3. The loopfor s in jsonResponse['results'] assignss to a dict for each iterationthrough the loop.
  4. s['geometry'] is a dict.
  5. s['geometry']['location'](finally!) contains thelatitude/longitude dict.
answeredJan 9, 2011 at 13:31
unutbu's user avatar
Sign up to request clarification or add additional context in comments.

Comments

0

First confusing it, cause the loads is not avaialble with import json, it has to be

import simplejson as json, so :

import urllibimport simplejson as jsonimport pprintURL2 = "http://pbx/a/kiosks"googleResponse = urllib.urlopen(URL2)jsonResponse = json.loads(googleResponse.read())pprint.pprint(jsonResponse)
answeredOct 22, 2013 at 17:52

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.