Python urllib2: Receive JSON response from url Python urllib2: Receive JSON response from url python python

Python urllib2: Receive JSON response from url


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

import urllib2import jsonresponse = urllib2.urlopen('https://api.instagram.com/v1/tags/pizza/media/XXXXXX')data = json.load(response)   print data


import jsonimport urlliburl = 'http://example.com/file.json'r = urllib.request.urlopen(url)data = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8'))print(data)

urllib, for Python 3.4
HTTPMessage, returned by r.info()


"""Return JSON to webpageAdding to wonderful answer by @SanalFor Django 3.4Adding a working url that returns a json (Source: http://www.jsontest.com/#echo)"""import jsonimport urlliburl = 'http://echo.jsontest.com/insert-key-here/insert-value-here/key/value'respons = urllib.request.urlopen(url)data = json.loads(respons.read().decode(respons.info().get_param('charset') or 'utf-8'))return HttpResponse(json.dumps(data), content_type="application/json")