title: How can I convert JSON to CSV?
body: <p>I have a JSON file I want to convert to a CSV file. How can I do this with Python?</p>
  <p>I tried:</p>
  <pre class="lang-py prettyprint-override"><code>import json
  import csv
  
  f = open('data.json')
  data = json.load(f)
  f.close()
  
  f = open('data.csv')
  csv_file = csv.writer(f)
  for item in data:
      csv_file.writerow(item)
  
  f.close()
  </code></pre>
  <p>However, it did not work. I am using Django and the error I received is:</p>
  <pre class="lang-none prettyprint-override"><code>`file' object has no attribute 'writerow'`
  </code></pre>
  <p>I then tried the following:</p>
  <pre class="lang-py prettyprint-override"><code>import json
  import csv
  
  f = open('data.json')
  data = json.load(f)
  f.close()
  
  f = open('data.csv')
  csv_file = csv.writer(f)
  for item in data:
      f.writerow(item)  # ← changed
  
  f.close()
  </code></pre>
  <p>I then get the error:</p>
  <pre class="lang-none prettyprint-override"><code>`sequence expected`
  </code></pre>
  <p>Sample json file:</p>
  <pre><code>[{
          &quot;pk&quot;: 22,
          &quot;model&quot;: &quot;auth.permission&quot;,
          &quot;fields&quot;: {
              &quot;codename&quot;: &quot;add_logentry&quot;,
              &quot;name&quot;: &quot;Can add log entry&quot;,
              &quot;content_type&quot;: 8
          }
      }, {
          &quot;pk&quot;: 23,
          &quot;model&quot;: &quot;auth.permission&quot;,
          &quot;fields&quot;: {
              &quot;codename&quot;: &quot;change_logentry&quot;,
              &quot;name&quot;: &quot;Can change log entry&quot;,
              &quot;content_type&quot;: 8
          }
      }, {
          &quot;pk&quot;: 24,
          &quot;model&quot;: &quot;auth.permission&quot;,
          &quot;fields&quot;: {
              &quot;codename&quot;: &quot;delete_logentry&quot;,
              &quot;name&quot;: &quot;Can delete log entry&quot;,
              &quot;content_type&quot;: 8
          }
      }, {
          &quot;pk&quot;: 4,
          &quot;model&quot;: &quot;auth.permission&quot;,
          &quot;fields&quot;: {
              &quot;codename&quot;: &quot;add_group&quot;,
              &quot;name&quot;: &quot;Can add group&quot;,
              &quot;content_type&quot;: 2
          }
      }, {
          &quot;pk&quot;: 10,
          &quot;model&quot;: &quot;auth.permission&quot;,
          &quot;fields&quot;: {
              &quot;codename&quot;: &quot;add_message&quot;,
              &quot;name&quot;: &quot;Can add message&quot;,
              &quot;content_type&quot;: 4
          }
      }
  ]
  </code></pre>