pythonjsongeojson

Calling a JSON API using Python


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.

API End Points

To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:

http://py4e-data.dr-chuck.net/json? This API uses the same parameter (address) as the Google API. This API also has no rate limit so you can test as often as you like. If you visit the URL with no parameters, you get "No address..." response.

I have worked on this assignment, but the codes are not running. I am JSONDecodeError whenever I run the program.

import urllib.request, urllib.parse, urllib.error
import json
adr= 'http://py4e-data.dr-chuck.net/json?'
while True:
    loca= input('Enter Location: ')
    if len(loca)<1:break

    url=adr + urllib.parse.urlencode({"address": loca})
    print('Retrieving', url)
    fha=urllib.request.urlopen(url)
    data=fha.read().decode()
    print('Retrieved', len(data))

    jsdata=json.loads(str(data))
    placeid= jsdata['results'][0]['place_id']
    print('The Place ID is: ', placeid)

Solution

  • It seems that the error is that this expects an extra parameter (Key)

    Missing/incorrect key = parameter (it is an easy number to guess) ...
    

    Edit: Check the documentation https://www.py4e.com/code3/geodata/README.txt

    Example 1: http://py4e-data.dr-chuck.net/json?key=42&address=Monash+University

    Example 2: http://py4e-data.dr-chuck.net/json?key=42&address=Kokshetau+Institute+of+Economics+and+Management