Last Updated: October 10, 2024
·
52.8K
· 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.close()

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.

2 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

over 1 year ago ·