Python error load JSON code of google API
Asked Answered
I

3

23

I am using google geocode API to test the following Python3.5 code but receive the error below.

raise JSONDecodeError("Expecting value", s, err.value) from None >JSONDecodeError: Expecting value

Here are the codes:

import urllib
import json

serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:
    address = input('Enter location: ')
    if len(address) < 1 : break

    url = serviceurl + urllib.parse.urlencode({'sensor':'false',
       'address': address})
    print ('Retrieving', url)
    uh = urllib.request.urlopen(url)
    data = uh.read()
    print ('Retrieved',len(data),'characters')

    js = json.loads(str(data))

Any idea about why I have the error.

Ibis answered 3/1, 2016 at 3:59 Comment(1)
This error is produced when you pass a variable that has None, blankstring or other incompatible datatype into json.loads(...) See: docs.python.org/3/library/json.html If you're loading json directly from a file you should be using myjson = json.load(open("yourjson.json", 'r')) which produces a python dict datatype in memory.Cynthea
M
3

So, I had to modify your code to run. I am using Python 3.4.3 on Ubuntu 14.04.

#import urllib  
import urllib.parse
import urllib.request

I received a similar error:

heyandy889@laptop:~/src/test$ python3 help.py 
Enter location: MI
Retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=MI
Retrieved 1405 characters
Traceback (most recent call last):
  File "help.py", line 18, in <module>
    js = json.loads(str(data))
  File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

Basically, instead of trying to decode the valid json string, we're trying to decode the Python 'None' value, which is not valid json. Try patching in the following example code. Run it first once to double-check that the simplest json object '{}' will work. Then, try each different 'possible_json_string' one by one.

#...
print ('Retrieved',len(data),'characters')

#possible_json_string = str(data) #original error
possible_json_string = '{}' #sanity check with simplest json
#possible_json_string = data #why convert to string at all?
#possible_json_string = data.decode('utf-8') #intentional conversion

print('possible_json_string')
print(possible_json_string)
js = json.loads(possible_json_string)

Source

Murrhine answered 3/1, 2016 at 5:9 Comment(0)
S
12

The error arises because the "data" is of type bytes so you have to decode it into a string before using json.loads to turn it into a json object. So to solve the problem:

uh = urllib.request.urlopen(url)
data = uh.read()
print ('Retrieved',len(data),'characters')

js = json.loads(data.decode("utf-8"))

Also, str(data) in the code you share will work in Python 2.x but not in Python 3.x because str() doesn't turn bytes into a string in 3.x.

Suburbanize answered 9/3, 2016 at 7:19 Comment(1)
thanks for sharing. Took me a while to find json.loads(data) in the trace back but I eventually did. As I use bot studio it is not a 100% fix.Judiciary
P
9

Look at the error:

"raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value"

It's saying I got a None when I was supposed to get something.

Check your data variable for None before calling the json.loads().

if data == None or data == '':
  print('I got a null or empty string value for data in a file')
else:
  js = json.loads(str(data))
Pelagianism answered 9/3, 2020 at 14:30 Comment(0)
M
3

So, I had to modify your code to run. I am using Python 3.4.3 on Ubuntu 14.04.

#import urllib  
import urllib.parse
import urllib.request

I received a similar error:

heyandy889@laptop:~/src/test$ python3 help.py 
Enter location: MI
Retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=MI
Retrieved 1405 characters
Traceback (most recent call last):
  File "help.py", line 18, in <module>
    js = json.loads(str(data))
  File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

Basically, instead of trying to decode the valid json string, we're trying to decode the Python 'None' value, which is not valid json. Try patching in the following example code. Run it first once to double-check that the simplest json object '{}' will work. Then, try each different 'possible_json_string' one by one.

#...
print ('Retrieved',len(data),'characters')

#possible_json_string = str(data) #original error
possible_json_string = '{}' #sanity check with simplest json
#possible_json_string = data #why convert to string at all?
#possible_json_string = data.decode('utf-8') #intentional conversion

print('possible_json_string')
print(possible_json_string)
js = json.loads(possible_json_string)

Source

Murrhine answered 3/1, 2016 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.