How to Convert a JSON String to CSV Using Python?

The data can be stored in a variety of formats, including XML, JSON, and CSV. Sometimes, it is required to convert data from one format to another, for example, to import data into a database, to make it more accessible to a wider range of applications, etc. Python provides numerous modules and functions to convert one data format into a different format.

This tutorial will explain different methods to convert JSON strings to CSV files using Python. The below content will be discussed with numerous examples:

Method 1: Using Pandas Module

The pandas module provides a function named “pd.read_json()” and “df.to_csv()” to read and write a JSON string to a CSV file in Python. 

The below code uses the “pandas.read_json()” function of the Pandas module to convert JSON string to CSV file:

Code: 

import pandas
json_value = '[{"Name": "Joseph", "Age": 23, "Height": 5.7}, {"Name": "Lily", "Age": 22, "Height": 6.7},{"Name": "Alex", "Age": 25, "Height": 5.6},{"Name": "Henry", "Age": 24, "Height": 6.1}]'
data_frame = pandas.read_json(json_value)
data_frame.to_csv('file.csv')
  • The “pandas” module is imported into the program.
  • The JSON string is initialized and assigned in a variable called “json_value”.
  • The “pd.read_json()” takes the JSON string as an argument and returns the DataFrame object.
  • The “df.to_csv()” function is used to write a DataFrame object to a CSV file named “file.csv”

Output: 

The given JSON string has been converted into a CSV file.

Method 2: Using CSV and JSON Module

To convert the JSON string to a CSV file you can use the “csv.writer()” function of the CSV module and the “json.loads()” function of the JSON module.

Here is an example:

Code: 

import json
import csv
json_value = '[{"Name": "Joseph", "Age": 23, "Height": 5.7}, {"Name": "Lily", "Age": 22, "Height": 6.7},{"Name": "Alex", "Age": 25, "Height": 5.6},{"Name": "Henry", "Age": 24, "Height": 6.1}]'
json_object = json.loads(json_value)
with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(json_object[0].keys())
    for row in json_object:
        writer.writerow(row.values())
  • In the above code, we import the json and csv modules.
  • The json string is initialized and assigned to a variable called “json_value”.
  • The “json.loads()” function accepts the JSON string object and transforms it into a Python object.
  • A “CSV” file named “output.csv” is opened/created in “w” (write) mode using the “with open()” function.
  • The “csv.writer()” function takes the file object and writes data to a CSV file.
  • The keys() method returns the keys of the object placed at the first index in the JSON array which is passed to the “writerow()” function.
  • The “writerow()” function accepts the first object of the json array and writes the header/first row to the CSV file named “output.csv”. 
  • The for loop is used to iterate through each object in the JSON array and write the values to the CSV file using the “row.values()” method.
  • The first row of the CSV file contains the header values, which are the keys of the first object in the JSON list.

Output: 

The JSON string has been converted into a CSV file.

Conclusion

The “pd.read_json()” and “df.to_csv()” functions of the Pandas module are used to convert/transform the JSON string to a CSV file in Python. The “pd.read_json()” function is used along with the“df.to_csv()” function to read the JSON data and convert the dataframe to the CSV file. Similarly, the “json.loads()” function parses the JSON data to a Python object, and the “csv.writer()” function writes the Python object to the CSV file. This guide provides various methods to convert JSON strings to CSV using appropriate examples in Python.