Coding Ref

Convert JSON to CSV in Python

Convert JSON to CSV in Python

To convert a JSON file to a CSV file in Python, use the pandas library.

Use the following code to convert your JSON file to a CSV file.

main.py
import pandas as pd

# 👇️ Read the json file
with open("example.json", encoding="utf-8") as input_file:
  input_dataframe = pd.read_json(input_file)

# 👇️ Use the pandas library to convert the pandas dataframe into a CSV
# 1. Return a string
input_dataframe.to_csv()

# 2. Save the CSV to a file
# `index` is set to `False` so that an extra index column is not created
input_dataframe.to_csv("../example_folder/example.csv", index=False)

Conclusion

To convert a JSON file to a CSV file in Python, use the pandas library.

You'll also like

Related tutorials curated for you

    TypeError: string indices must be integers

    SyntaxError: unexpected EOF while parsing

    How to fix: Can only use .str accessor with string values, which use np.object_ dtype in pandas

    ModuleNotFoundError: No module named 'cv2' in Python

    ModuleNotFoundError: No module named 'pandas' in Python

    Syntax Error: invalid syntax

    TypeError: a bytes-like object is required, not 'str'

    TypeError: only integer scalar arrays can be converted to a scalar index

    How to fix: pandas data cast to numpy dtype of object. Check input data with np.asarray(data)

    ModuleNotFoundError: No module named 'pip' in Python

    How to fix: IndentationError expected an indented block in Python

    ValueError: setting an array element with a sequence