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:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s