Python urllib2: Receive JSON response from url


I am trying to GET a URL using Python and the response is JSON. However, when I run

import urllib2
response = urllib2.urlopen('')
print html

The html is of type str and I am expecting a JSON. Is there any way I can capture the response as JSON or a python dictionary instead of a str.

12/17/2012 8:46:07 PM

Accepted Answer

If the URL is returning valid JSON-encoded data, use the json library to decode that:

import urllib2
import json

response = urllib2.urlopen('')
data = json.load(response)   
print data
12/17/2012 8:47:46 PM

import json
import urllib

url = ''
r = urllib.request.urlopen(url)
data = json.loads('charset') or 'utf-8'))

urllib, for Python 3.4
HTTPMessage, returned by

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow