Register Login

How to convert JSON to CSV in Python?

Updated Jun 24, 2021

JSON (JavaScript Object Notation) is a popular data storing and transferring tool used in many server-side programming. Python supports JSON using its in-built package called json. We can convert a JSON structure into a comma-delimited textual structure. In this article, you will learn about the different methods to convert JSON to CSV structure.

What are CSV files?

CSV (Comma Separated Values) is a file format utilized for storing data in a tabular fashion. The structure of a CSV file can be interpreted in a spreadsheet or database format. It can store data in plain text (usually, string & number data type). Each line of a .csv file defines a data record. The record will contain one or more fields that are comma-separated values.

Converting JSON to CSV

JSON data usually contains data in key-value pairs. These keys will be the headers for the CSV file and the values as descriptive data that remain indented in json.

Method 1: Using CSV module:

It is a built-in Python module that implements classes for reading & writing tabular data in CSV structure. Using this, programmers can write this data in the format approved by Excel or read data from excel or CSV files. Programmers and developers can also represent the CSV formats recognized by other apps or define their special-purpose CSV formats. It is the most widely used module. Since it is a built-in module, you do not need to install it separately.

Program:

import json
import csv

# Open the JSON file & load its data
with open('data_file.json') as dat_file:
    data = json.load(dat_file)
stud_data = data['stud_details']
 
# Opening a CSV file for writing in write mode
data_file = open('data_file.csv', 'w') 
csv_writer = csv.writer(data_file)

count = 0 
for cnt in stud_data:
    if count == 0:
        header = cnt.keys()
        csv_writer.writerow(header)
        count += 1
    csv_writer.writerow(cnt.values())
data_file.close()

Explanation:

First, we will import the json and csv modules. Next, we will open the JSON file & load its data in the 'data' object. Next, we have to open a CSV file for writing in write mode. We assigned the count variable to zero. Then, use the for loop to fetch the data from the stud_data. Now, assign the cnt.keys() in the header. Provide the csv_writer.writerow() method and pass the header as the argument. Once you count the values, you should close the data_file.close().

Method 2: Using Pandas Library:

It is a fast, flexible, powerful, & easy to implement open-source data analysis tool developed on top of Python language. It is a data manipulation & analysis library that worked well with conversations and various files like CSV, JSON, etc. Here, we will use the to_scv() method to convert a file to CSV.

Program:

import pandas as pd
from pathlib import Path
import json

# absolute path to json file
jsonpath = Path('file.json')

# reading the json file
with jsonpath.open('r', encoding='utf-8') as dat_f:
    dat = json.loads(dat_f.read())

# creating the dataframe
df = pd.json_normalize(dat)

# converted a file to csv
df.to_csv('datafile.csv', encoding='utf-8', index=False)

Explanation:

First, we will import the pandas library, path, and json modules. Fetch the json file path using the Path() constructor and assign it to the jsonpath object. Next, you have to read the json file and assign it to the 'dat' object. Then, create the Dataframe using json_normalize() method and pass the 'dat' object into it as the parameter. Finally, convert the df.to_csv() method to convert the dataframe object to csv format with the filename 'datafile.csv', having encoding scheme as UTF-8, and index as False.

Conclusion:

Both these methods are used to convert a JSON structure to a CSV file. But using the CSV module is beneficial because it is a built-in module and hence lighter as compare to the Pandas library. Also, the dataframe conversion takes more time making it complex. Hence, the first method should be preferred.


×