Notes

How to parse JSON data in Python

Edit on GitHub

Python
1import urllib2
2import json
3
4url = urllib2.urlopen("http://portfolio.us.reuters.com/us/api/PortfolioSpy.asp?symbol=APL.KA&format=json")
5
6data = json.load(url)
7
8print data

We need to do two steps:

  1. Open the URL
  2. Parse the JSON data

Open the URL

To open URLs in Python we need the urllib2 module. Import the module in your script by adding import urllib2

With the .urlopen function we open the URL, which can be either a string or a Request object.

Parse the JSON data

To parse JSON data in Python we need the json module.

With the .load function we deserilaize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.