I am trying to make an API call to http://api.stackoverflow.com/1.1/badges/name
My code snippet –
url = 'http://api.stackoverflow.com/1.1/badges/name'
f = urllib2.urlopen(url)
content = f.read()
jsonobj = json.loads(content)
print jsonobj
This gives me the error –
ValueError: No JSON object could be decoded
When I tried http://jsonviewer.stack.hu to load the json object from the above URL, it showed me garbled characters. You can see the output here – http://jsonviewer.stack.hu/#http://api.stackoverflow.com/1.1/badges/name
The text is displayed normally in the browser window if you go to http://api.stackoverflow.com/1.1/badges/name
I tried adding UTF-8 encoding –
jsonobj = json.loads(content, encoding = 'UTF-8')
but it still gives the same error.
According to http://api.stackoverflow.com/1.0/usage the returned information is gzipped. You will have to unzip the binary data to get the actual JSON. You can do this with the
gzipandStringIOmodules: