pythonjsonurllibcoursera-api

JSONDecodeError: Expecting value: line 1 column 1 (char 0)"


Here is the code..

address_input = input("Enter location: ")
params = {"sensor": "false", "address": address_input}
serviceurl = "http://py4e-data.dr-chuck.net/json?"
url = serviceurl + urllib.parse.urlencode(params)
print("Retrieving ", url)
data = urllib.request.urlopen(url).read().decode('utf-8')
print('Retrieved', len(data), 'characters')
json_obj = json.loads(data)
place_id = json_obj["results"][0]["place_id"]
print("Place id", place_id)

and here is the error that i am getting

here is the second image


Solution

  • Requires:

    Code

    import json
    import urllib
    import requests
    
    address_input = input("Enter location: ")
    params = {"sensor": "false", "address": address_input, "key": 42}
    
    serviceurl = "http://py4e-data.dr-chuck.net/json?"
    url = serviceurl + urllib.parse.urlencode(params)
    
    print("Retrieving ", url)
    resp = requests.get(url)
    print('Retrieved', len(resp.content), 'characters')
    
    json_obj = json.loads(resp.content)
    place_id = json_obj["results"][0]["place_id"]
    
    print("Place id", place_id)
    

    Output

    Enter location: UK
    Retrieving  http://py4e-data.dr-chuck.net/json?sensor=false&address=UK&key=42
    Retrieved 1196 characters
    Place id ChIJqZHHQhE7WgIReiWIMkOg-MQ