Python error load JSON code of google API

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.

Leave a Comment