title: Pretty-Print JSON Data to a File using Python
body: <p>A project for class involves parsing Twitter JSON data. I'm getting the data and setting it to the file without much trouble, but it's all in one line. This is fine for the data manipulation I'm trying to do, but the file is ridiculously hard to read and I can't examine it very well, making the code writing for the data manipulation part very difficult. </p>
  
  <p>Does anyone know how to do that from within Python (i.e. not using the command line tool, which I can't get to work)? Here's my code so far:</p>
  
  <pre><code>header, output = client.request(twitterRequest, method="GET", body=None,
                              headers=None, force_auth_header=True)
  
  # now write output to a file
  twitterDataFile = open("twitterData.json", "wb")
  # magic happens here to make it pretty-printed
  twitterDataFile.write(output)
  twitterDataFile.close()
  </code></pre>
  
  <p><strong>Note</strong> I appreciate people pointing me to simplejson documentation and such, but as I have stated, I have already looked at that and continue to need assistance. A truly helpful reply will be more detailed and explanatory than the examples found there. <em>Thanks</em></p>
  
  <p><strong>Also:</strong>
  Trying this in the windows command line:</p>
  
  <pre><code>more twitterData.json | python -mjson.tool &gt; twitterData-pretty.json
  </code></pre>
  
  <p>results in this:</p>
  
  <pre><code>Invalid control character at: line 1 column 65535 (char 65535)
  </code></pre>
  
  <p>I'd give you the data I'm using, but it's very large and you've already seen the code I used to make the file.</p>