. Advertisement .
..3..
. Advertisement .
..4..
I am working on python, but I found the following warning message:
raise JSONDecodeError("Expecting value", s, err.value) from None >JSONDecodeError: Expecting value
Is there any way to stabilize the issue “raise jsondecodeerror(“expecting value”, s, err.value) from none”? I read a lot of topics about this, but all of them were trying to install anything. Is this the correct way, or any recommendation for me? Please find the beginning command below:
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))
The cause: The “data” is of type bytes, so you must decode it into a string before using
json.loads
to transform it into a json object in order to avoid the error.Solution: You should decode the Python ‘None’ value, which is not acceptable json, rather than attempting to decode the valid json string. Try patching in the example code below. Run it once first to confirm that the simplest JSON object ‘{}’ will function. Next, try each ‘possible json string’ individually.
This error occurs because “data” is a type bytes. You will need to decode it to a string, before you use
json.loads
to convert it to a json objects. This will solve the problem.Additionally, str(data), in the code that you share with Python 2.x will work but not in Python 3.x. This is because str() does not convert bytes into strings in Python 3.x.