× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to load & parse JSON file in python

Martijn Pieters
Sep 05, 2014
<p>You need to parse your file line by line:</p><pre><code><span style="color:rgb(0, 0, 139); font-size:14px">import</span><span style="font-size:14px"> json data </span><span style="font-size:14px">=</span><span style="font-size:14px"> </span><span style="font-size:14px">[]</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">with</span><span style="font-size:14px"> open</span><span style="font-size:14px">(</span><span style="color:rgb(128, 0, 0); font-size:14px">'file'</span><span style="font-size:14px">)</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">as</span><span style="font-size:14px"> f</span><span style="font-size:14px">:</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">for</span><span style="font-size:14px"> line </span><span style="color:rgb(0, 0, 139); font-size:14px">in</span><span style="font-size:14px"> f</span><span style="font-size:14px">:</span><span style="font-size:14px"> data</span><span style="font-size:14px">.</span><span style="font-size:14px">append</span><span style="font-size:14px">(</span><span style="font-size:14px">json</span><span style="font-size:14px">.</span><span style="font-size:14px">loads</span><span style="font-size:14px">(</span><span style="font-size:14px">line</span><span style="font-size:14px">))</span></code></pre><p>Each <em>line</em> contains valid JSON, but as a whole, it is not a valid JSON value as there is no top-level list or object definition.</p><p>Note that because the file contains JSON per line, you are saved the headaches of trying to parse it all in one go or to figure out a streaming JSON parser. You can now opt to process each line separately before moving on to the next, saving memory in the process. You probably don't want to append each result to one list and <em>then</em> process everything if your file is really big.</p><p>If you have a file containing individual JSON objects with delimiters in-between, use <a href="http://stackoverflow.com/questions/21708192/how-do-i-use-the-json-module-to-read-in-one-json-object-at-a-time/21709058#21709058">How do I use the json module to read in one json object at a time?</a> to parse out individual objects using a buffered method.</p><p>This tip was originally posted on <a href="http://stackoverflow.com/questions/12451431/loading-parsing-json-file-in-python/12451465#12451465">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus