Open In App

Convert JSON to CSV in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-string which contains the value in key-value mapping within { }. It is similar to the dictionary in Python.
 

CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.
 

Refer to the below articles to understand the basics of JSON and CSV. 
 

 

Converting JSON to CSV

For simple JSON data consisting of key and value pairs, keys will be headers for the CSV file and values the descriptive data.
 

Example: Suppose the JSON file looks like this:
 

python-json-to-csv

We want to convert the above JSON to CSV file with key as headers.
 

Python3




# Python program to convert
# JSON file to CSV
 
 
import json
import csv
 
 
# Opening JSON file and loading the data
# into the variable data
with open('data.json') as json_file:
    data = json.load(json_file)
 
employee_data = data['emp_details']
 
# now we will open a file for writing
data_file = open('data_file.csv', 'w')
 
# create the csv writer object
csv_writer = csv.writer(data_file)
 
# Counter variable used for writing
# headers to the CSV file
count = 0
 
for emp in employee_data:
    if count == 0:
 
        # Writing headers of CSV file
        header = emp.keys()
        csv_writer.writerow(header)
        count += 1
 
    # Writing data of CSV file
    csv_writer.writerow(emp.values())
 
data_file.close()


Output:
 

python-json-to-csv-output

And, if you are working on JSON data like the following:

[

    {‘Age’: 18.0, ‘Salary’: 20000.0, ‘Gender’: ‘Male’, ‘Country’: ‘Germany’, ‘Purchased’: ‘N’}

    {‘Age’: 19.0, ‘Salary’: 22000.0, ‘Gender’: ‘Female’, ‘Country’: ‘France’, ‘Purchased’: ‘N’}

    {‘Age’: 20.0, ‘Salary’: 24000.0, ‘Gender’: ‘Female’, ‘Country’: ‘England’, ‘Purchased’: ‘N’}

]

The below code will work perfectly for you (please format the code before running)

Python3




import json
import csv
 
with open('G:\Akhil\jsonoutput.json') as json_file:
    jsondata = json.load(json_file)
 
data_file = open('G:\Akhil\jsonoutput.csv', 'w', newline='')
csv_writer = csv.writer(data_file)
 
count = 0
for data in jsondata:
    if count == 0:
        header = data.keys()
        csv_writer.writerow(header)
        count += 1
    csv_writer.writerow(data.values())
 
data_file.close()


 



Last Updated : 29 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads