Skip to content

Instantly share code, notes, and snippets.

@manichabba
Created August 20, 2016 03:21
Show Gist options
  • Save manichabba/c851535b2c6d6e39b5f84ec16b3e96e9 to your computer and use it in GitHub Desktop.
Save manichabba/c851535b2c6d6e39b5f84ec16b3e96e9 to your computer and use it in GitHub Desktop.
=====Calling a JSON API=====The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.
import urllib
import json
serviceurl = 'http://python-data.dr-chuck.net/geojson?'
while True:
address = raw_input('Enter location: ')
if len(address) < 1 : break
url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
try: js = json.loads(str(data))
except: js = None
if 'status' not in js or js['status'] != 'OK':
print '==== Failure To Retrieve ===='
print data
continue
print json.dumps(js, indent=4)
place = js['results'][0]['place_id']
print 'The place ID is:',place
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment