Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I was working on some online assignment, there I need to work on a program that will prompt for a location, contact a web service and recover JSON for the web service and parse that information, and retrieve the first place_id from the JSON. A place ID is an identifier(textual ) that uniquely distinguishes a place within Google Maps. API End Points

To finish this assignment, we should use this API endpoint that has a static subset of the Google Data.

I have worked on this assignment, but the codes are not operating. I am getting JSONDecodeError whenever I execute 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)

1 Answer

0 votes
by (108k points)
edited by

It appears that the error is that this requires 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

For more information regarding the python basics, do refer to the Python certification course that will help you out in a better way.

Related questions

+2 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Jul 15, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 5, 2019 in Python by Sammy (47.6k points)

Browse Categories

...