Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I convert JSON to CSV?
    primarykey
    data
    text
    <p>I have a JSON file that I want to covert to a CSV file. How can I do this with Python?</p> <p>I tried:</p> <pre><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) f.close() </code></pre> <p>However, it did not work. I am using Django and the error I received is:</p> <pre><code>file' object has no attribute 'writerow' </code></pre> <p>So, then I tried the following:</p> <pre><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>I then get the error:</p> <pre><code>sequence expected </code></pre> <p>Sample json file:</p> <pre class="lang-json prettyprint-override"><code>[ { "pk": 22, "model": "auth.permission", "fields": { "codename": "add_logentry", "name": "Can add log entry", "content_type": 8 } }, { "pk": 23, "model": "auth.permission", "fields": { "codename": "change_logentry", "name": "Can change log entry", "content_type": 8 } }, { "pk": 24, "model": "auth.permission", "fields": { "codename": "delete_logentry", "name": "Can delete log entry", "content_type": 8 } }, { "pk": 4, "model": "auth.permission", "fields": { "codename": "add_group", "name": "Can add group", "content_type": 2 } }, { "pk": 10, "model": "auth.permission", "fields": { "codename": "add_message", "name": "Can add message", "content_type": 4 } } ] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload