<p>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:</p>
<pre><code>{
&quot;name&quot;:&quot;tux&quot;,
&quot;health&quot;:&quot;23&quot;,
&quot;level&quot;:&quot;4&quot;
}</code></pre>
<p>If you are like me, three questions probably spring to your mind:</p>
<ol type="1">
<li><p><em>That looks an awful lot like a Python dictionary.</em></p>
<p>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.</p></li>
<li><p><em>I don't feel comfortable with dictionaries, can't I just use a delimited text file?</em></p>
<p>You can, but you will have to write parsers for it yourself. If your data gets very complex, the parsing can get pretty ugly.</p>
<p>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.</p>
<p>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.</p></li>
<li><p><em>Why not use XML instead?</em></p>
<p>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.</p></li>
</ol>
<p>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.</p>
<p>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.</p>
<h2 id="storing-data-as-json">Storing Data as JSON</h2>
<p>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:</p>
<pre><code>#!/usr/bin/env python

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

game[&#39;konqi&#39;] = {&#39;health&#39;: 18, &#39;level&#39;: 7}</code></pre>
<p>That code creates a ditionary called <strong>game</strong> 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.</p>
<p>Now let's see how to save that data to a save file.</p>
<pre><code>## continued...
import json

with open(&#39;dosiero.json&#39;, &#39;w&#39;) as outfile:
    json.dump(game, outfile)</code></pre>
<p>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.</p>
<h2 id="reading-data-from-a-json-file">Reading Data from a JSON File</h2>
<p>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 <strong>json.load</strong></p>
<pre><code>import json

dosiero = open(&#39;dosiero.json&#39;)
game = json.load(dosiero)

print game[&#39;tux&#39;]     # prints {&#39;health&#39;: 23, &#39;level&#39;: 4}
print game[&#39;tux&#39;][&#39;health&#39;]    # prints 23
print game[&#39;tux&#39;][&#39;level&#39;]     # prints 4

# when finished, close the file

json_data.close()</code></pre>
<p>As you can see, JSON integrates surprisingly well with Python, so it's a great format when your data fits in with its model.</p>
<p>Have fun!</p>
<p>[EOF]</p>
<p>Made with Free Software.</p>
