Main Content
JSON from Python - first principles, easy example Archive - Originally posted on "The Horse's Mouth" - 2013-05-13 18:53:37 - Graham Ellis
Here's a short example of how to pick up a JSON feed from a URL in Python. All the examples I came across looked very complicated, so I thought I would write one that's really straightforward:
Open a remote URL feed that provides a JSON object:
response = urllib2.urlopen('http://www.wellho.net/services/pix.json')
Read that object in:
stuff = response.read()
That'll give you a dict... and you can look at the keys:
for itemname in memory.keys():
print itemname
... you can get at individual values:
toby = time.gmtime(memory["timedat"])
print "This was generated at",memory["timedat"],time.strftime("%c",toby)
if memory["lookfor"]:
print "Searching only for",memory["lookfor"]
else:
print "All available records requested"
... and you can look deeper into the structure if you like:
for record in memory["acc"]:
print record["when"], record["which"]
Full code [here] . More about urllib2, JSON from Pyton and lots of other subjects on our Intermediate Python Course
Some other articles
Y117 - Already written modules Searching a Json or XML structure for a specific key / value pair in Python Scons - a build system in Python - building hello world Month, Day, Year number to day of week and month names in Python - English and Swedish Programming with random numbers - yet re-using the same values for testing Binary data handling - Python and Perl Reading command line parameters in Python Cacheing class for Python - using a local SQLite database as a key/value store This article Practical Extraction and Reporting - using Python and Extreme Programming How can I do an FTP transfer in Python? A demonstration of how many Python facilities work together Syncronise - software, trains, and buses. Please! Dates and times in Python Good example of recursion in Python - analyse an RSS feed Learning Python - many new example programs Y115 - Additional Python Facilities Some gems from Intermediate Python Command line parameter handling in Python via the argparse module Json load from URL, recursive display, Python 3.4 Running an operating system command from your Python program - the new way with the subprocess module Json is the new marshall, pickle and cPickle / Python Python - an interesting application Handling JSON in Python (and a csv, marshall and pickle comparison) This article Teaching dilemma - old tricks and techniques, or recent enhancements? A demonstration of how many Python facilities work together Python regular expressions - repeating, splitting, lookahead and lookbehind Joining a MySQL table from within a Python program Factory methods and SqLite in use in a Python teaching example Running operating system commands from your Python program Python decorators - your own, staticmethod and classmethod Model - View - Controller demo, Sqlite - Python 3 - Qt4 Connecting Python to sqlite and MySQL databases Regular Expressions in Python Python - what is going on around me? Python - how it saves on compile time Serialization - storing and reloading objects Testing code in Python - doctest, unittest and others Python Regular Expressions A series of tyre damages Ignore case in Regular Expression Regular expressions made easy - building from components Turning objects into something you can store - Pickling (Python) Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP Sending an email from Python Python - listing out the contents of all variables Python 3000 - the next generation Keeping your regular expressions simple Python to MySQL Splitting the difference What and why for the epoch Examples - Gadfly, NI Number, and Tcl to C interface The elegance of Python