On php, I just use json_encode() and json_decode() to handle JSON data with ease. Python has a built in module named “json” for the same purpose. Here’s the link for the official documentation: http://docs.python.org/library/json.html 🙂 The documentation has pretty examples. Still, I wanted to try things out by myself. I used Python to parse the JSON output of a certain Yahoo! Pipes feed :
1 2 3 4 5 |
import json,urllib data = urllib.urlopen("http://pipes.yahoo.com/pipes/pipe.run?_id=a0b9e3295664870ee81711aed6f7ecd6&_render=json").read() d = json.loads(data) for x in d['value']['items']: print x['title'] |
Output:
1 2 3 4 5 6 7 8 9 10 |
masnun@ubuntu:~/Desktop$ python jsontest.py Sms Plugin .......... [SNIP] .......... Profit Spreadsheet Graphic Web Des. Needed Fast Customer Ordering Website Dating Site Profiles masnun@ubuntu:~/Desktop$ |
2 replies on “Handling JSON in Python”
[…] This post was mentioned on Twitter by Abu Ashraf Masnun, Telletto. Telletto said: maSnun says… Handling JSON in Python: On php, I just use json_encode() and json_decode() to handle JSON data wit… http://bit.ly/aGfmoB […]
Just have to say thank you because I have spent few weeks trying to parse information from my JSON file and your example helped me realise I was working with wrong entries! Thank you so much!