Sometimes, we want to convert JSON to CSV with Python.
In this article, we’ll look at how to convert JSON to CSV with Python.
How to convert JSON to CSV with Python?
To convert JSON to CSV with Python, we can use the Panda’s read_json
method to convert a JSON string into a data frame.
Then we use to_csv
to convert the data frame to a csv.
For instance, we write
import pandas as pd
with open('jsonfile.json', encoding='utf-8') as input_file:
df = pd.read_json(input_file)
df.to_csv('csvfile.csv', encoding='utf-8', index=False)
to call pd.read_json
with input_file
to read the JSON file into a data frame.
Then we call to_csv
with the file path to write to, the encoding, and we skip the index with index
set to False
to write the data frame content into a csv file.
Conclusion
To convert JSON to CSV with Python, we can use the Panda’s read_json
method to convert a JSON string into a data frame.
Then we use to_csv
to convert the data frame to a csv.