Python – Read and parse a JSON via URL

Here is an example Python program to read a JSON via URL using urllib2 and simplejson.

import urllib2
import simplejson

response = urllib2.urlopen("http://172.0.0.1:8000/custom/get/")
data = simplejson.load(response)
print data
# => {'content': 'Hello World!', 'success': True}

 

If you want to get the value of a specific key

import urllib2
import simplejson

response = urllib2.urlopen("http://172.0.0.1:8000/custom/get/")
data = simplejson.load(response)
print data["content"]
# => Hello World!

 

Done =)

References:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.