Site Map - skip to main content - dyslexic font - mobile - text - print

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.

hpr2010 :: Parsing JSON with Python

How to parse JSON with Python

<< First, < Previous, Latest >>

Hosted by klaatu on 2016-04-15 is flagged as Clean and is released under a CC-BY-SA license.
Listen in ogg, spx, or mp3 format. | Comments (3)

Part of the series: A Little Bit of Python

Initially based on the podcast "A Little Bit of Python", by Michael Foord, Andrew Kuchling, Steve Holden, Dr. Brett Cannon and Jesse Noller.

Now the series is open to all.

JSON is a popular way of storing data in a key/value type arrangement so that the data can be parsed easily later. For instance, here is a very simple JSON snippet:


If you are like me, three questions probably spring to your mind:

  1. That looks an awful lot like a Python dictionary.

    Yes, it looks exactly like a Python dictionary. They are shockingly similar. If you are comfortable with Python lists and dictionaries, you will feel right at home with JSON.

  2. I don't feel comfortable with dictionaries, can't I just use a delimited text file?

    You can, but you will have to write parsers for it yourself. If your data gets very complex, the parsing can get pretty ugly.

    That is not to say that you should not use a simple delimited text file if that is all that your programme needs. For example, I would not want to open a config file as a user and find that I have to format all my options as valid JSON.

    Just know that JSON is out there and available, and that the JSON Python module has some little features that make your life easier when dealing with sets of data.

  3. Why not use XML instead?

    You can. Mostly one should use the most appropriate format for one's project. I'm a big fan of XML, but sometimes JSON makes more sense.

I am not going to make this post about teaching the JSON format. If you need clarification on how to structure data into JSON, go through a tutorial on it somewhere; there are several good ones online. Honestly, it's not that complex; you can think of JSON as nested dictionaries.

Starting from scratch, let's say that you write a programme that by nature gathers data as it runs. When the user quits, you want to save the data to a file so that when the user resumes the app later, they can load the file back in and pick up where they left off.

Storing Data as JSON

At its most basic, the JSON data structure is basically the same as a Python dictionary, and in fact the nice thing about JSON is that it can be directly imported into a Python dictionary. Usually, however, you are resorting to JSON because you have somewhat complex data, so in the sample code we will use a dictionary-within-a-dictionary:

#!/usr/bin/env python

game = {'tux': {'health': 23, 'level': 4}, 'beastie': {'health': 13, 'level': 6}}
# you can always add more to your dictionary

game['konqi'] = {'health': 18, 'level': 7}

That code creates a ditionary called game which stores the player name and a corresponding dictionary of attributes about how the player is doing in the progress of the game. As you can see after the comment, adding new players is simple.

Now let's see how to save that data to a save file.

## continued...
import json

with open('dosiero.json', 'w') as outfile:
    json.dump(game, outfile)

That would be your save command. Simple as that, all the structured content of your game dictionary is committed to a file on your hard drive.

Reading Data from a JSON File

If you are saving data to JSON, you probably will evenually want to read the data back into Python. For this, Python features the function json.load

import json

dosiero = open('dosiero.json')
game = json.load(dosiero)

print game['tux']     # prints {'health': 23, 'level': 4}
print game['tux']['health']    # prints 23
print game['tux']['level']     # prints 4

# when finished, close the file


As you can see, JSON integrates surprisingly well with Python, so it's a great format when your data fits in with its model.

Have fun!


Made with Free Software.


Subscribe to the comments RSS feed.

Comment #1 posted on 2016-04-15T02:44:45Z by Zen_Floater2


Glad to hear you didn't just pass away or go back to urban camping.
Frankly, I I'm surprised Plasma 5 is still not ready yet for Slackware, or anybody.
But that's really no reason to go hide in a cave young man.

Comment #2 posted on 2016-04-22T14:33:35Z by Ken Fallon

Don't like xpath !

What - You don't like XPath !

Why if only someone recorded a show about that.

/me digs his own hole on this one.


Comment #3 posted on 2016-05-24T13:23:54Z by rstackhouse

JSON's rise in popularity was due to its utility as a data transfer format in heavy client web applications. XML is very verbose in comparison with JSON. Back when JavaScript interpreters were slower, this bloat was a big deal. XML just takes longer to parse, and in an environment where type coercion is the norm, a lot of type information, in the form of XSD, just doesn't make sense. When you own both ends of a communication pipeline, a strict contract, isn't really necessary.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to
record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Anti Spam Question: What does the P in HPR stand for ?
Are you a spammer →
Who hosted this show →
What does HPR mean to you ?