How to parse through JSON object in Python -
i know there tons of exact question on here can't find answer.
i'm trying use google maps api geocoding go through list of sloppy addresses, , formatted addresses.
i have exact code google documentation:
import urllib2 address="1600+amphitheatre+parkway,+mountain+view,+ca" url="https://maps.googleapis.com/maps/api/geocode/json?address=%s" % address response = urllib2.urlopen(url) jsongeocode = response.read()
it says jsongeocode
json object. however, i've gathered looking online not json object yet. if i'm wrong here , json object, how parse it?
if call print(jsongeocode)
prints appears json object proper attributes in terminal.
so try convert jsongeocode
object json object:
jdata = json.load(jsongeocode)
this gives me error:
attributeerror: 'bytes' object has no attribute 'read'
edit
i've switched json function call jdata = json.loads(jsongeocode)
, error this:
typeerror: json object must str, not 'bytes'
your code works fine in python 2. need parse json string returned api using json.loads(jsongeocode)
.
the error message typeerror: json object must str, not 'bytes'
suggests me using python 3. however, importing urllib2
module present in python 2. i'm not sure how achieved that. anyway, assuming python 3:
import json urllib.request import urlopen address="1600+amphitheatre+parkway,+mountain+view,+ca" url="https://maps.googleapis.com/maps/api/geocode/json?address=%s" % address response = urlopen(url) json_byte_string = response.read() >>> type(json_byte_string) <class 'bytes'> >>> jdata = json.loads(json_byte_string) traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib64/python3.4/json/__init__.py", line 312, in loads s.__class__.__name__)) typeerror: json object must str, not 'bytes'
so there exception have seen. response contained in json_byte_string
byte string - of class bytes
, however, in python 3 json.loads()
expects str
unicode string. need convert json_byte_string
byte string unicode. requires know encoding of byte string. here use utf8 because specified in content-type
response header:
>>> response.headers['content-type'] 'application/json; charset=utf-8' >>> json_string = str(json_byte_string, 'utf8') >>> type(json_string) <class 'str'>
now can passed json.loads()
:
>>> jdata = json.loads(json_string) >>> jdata {'results': [{'types': ['street_address'], 'address_components': [{'types': ['street_number'], 'long_name': '1600', 'short_name': '1600'}, {'types': ['route'], 'long_name': 'amphitheatre parkway', 'short_name': 'amphitheatre pkwy'}, {'types': ['locality', 'political'], 'long_name': 'mountain view', 'short_name': 'mountain view'}, {'types': ['administrative_area_level_2', 'political'], 'long_name': 'santa clara county', 'short_name': 'santa clara county'}, {'types': ['administrative_area_level_1', 'political'], 'long_name': 'california', 'short_name': 'ca'}, {'types': ['country', 'political'], 'long_name': 'united states', 'short_name': 'us'}, {'types': ['postal_code'], 'long_name': '94043', 'short_name': '94043'}], 'formatted_address': '1600 amphitheatre parkway, mountain view, ca 94043, usa', 'geometry': {'location_type': 'rooftop', 'viewport': {'northeast': {'lng': -122.0828811197085, 'lat': 37.4236854802915}, 'southwest': {'lng': -122.0855790802915, 'lat': 37.4209875197085}}, 'location': {'lng': -122.0842301, 'lat': 37.4223365}}, 'place_id': 'chij2eugeak6j4arbn5u_wagqwa'}], 'status': 'ok'}
and there have decoded json string dictionary. formatted address available as:
>>> jdata['results'][0]['formatted_address'] '1600 amphitheatre parkway, mountain view, ca 94043, usa'
there better way this: use requests
:
import requests address="1600+amphitheatre+parkway,+mountain+view,+ca" url="https://maps.googleapis.com/maps/api/geocode/json?address=%s" % address r = requests.get(url) jdata = r.json() >>> jdata['results'][0]['formatted_address'] '1600 amphitheatre parkway, mountain view, ca 94043, usa'
much better!
Comments
Post a Comment