Last Updated: October 13, 2018
· cboji

How to convert json to csv (excel).

In order to get data from json to csv you can use the script below:

import json
import csv

f = open('file.json')
data = json.load(f)

f = csv.writer(open('file.csv', 'wb+'))
# use encode to convert non-ASCII characters
for item in data:
    values = [ x.encode('utf8') for x in item['fields'].values() ]
    f.writerow([item['pk'], item['model']] + values)

PS: To get excel format you can just open file.csv in redactor and save as xlsx or you can do the same action using google-drive.

3 Responses
Add your response


I'm completely new to Python,
This error is reported
values = [ x.encode('utf8') for x in item['fields'].values() ]
TypeError: string indices must be integers

over 1 year ago ·

You should consider looking at pandas for this stuff..
It's pretty cool

9 months ago ·

You can fully automate the JSON to CSV conversion process with Flexter our free JSON converter. Flexter is an ETL tool for JSON and XML. It automates the conversion of JSON to a database, text, or Hadoop. When you convert your JSON files it also provides a diagram of the target model and the data lineage. It can handle JSON of any complexity.

Try for yourself: https://jsonconverter.sonra.io/

9 months ago ·