How to parse JSON data in Python
import urllib2
import json
url = urllib2.urlopen("http://portfolio.us.reuters.com/us/api/PortfolioSpy.asp?symbol=APL.KA&format=json")
data = json.load(url)
print data
We need to do two steps:
- Open the URL
- 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.