In This Article
In this tutorial, we’ll guide you through the process of converting JSON to CSV in Python. Whether you’re a beginner or an experienced coder, we’ll make this process easy to understand with step-by-step instructions and code examples.
Step 1: Import Necessary Libraries
To convert JSON data to CSV file using Python, you can use libraries such as json
and csv
.
We’ll also introduce the use of pandas
for more advanced data manipulation. Start by importing these libraries in your Python script.
import json
import csv
import pandas as pd
Step 2: Read JSON Data
Now, let’s open and read the JSON file you want to convert into CSV using Python. Replace "input.json"
with the path to your JSON file.
with open("input.json", "r") as json_file:
data = json.load(json_file)
Here is sample input JSON file data that we will convert to CSV.
[
{
"field1": "Value1A",
"field2": "Value1B",
"field3": "Value1C"
},
{
"field1": "Value2A",
"field2": "Value2B",
"field3": "Value2C"
},
{
"field1": "Value3A",
"field2": "Value3B",
"field3": "Value3C"
}
]
Note: Make sure you keep the input.json file in the same directory folder.
Step 3: Create a CSV File
Next, create a CSV file where you’ll write the converted data. You can specify the file name as needed.
csv_file = open("output.csv", "w", newline="")
csv_writer = csv.writer(csv_file)
Step 4: Write CSV Headers
Before writing the data, you should write the headers (column names) to your CSV file. This step is optional but useful for data organization.
# Replace 'fieldnames' with your desired column names
fieldnames = ["column1", "column2", "column3"]
csv_writer.writerow(fieldnames)
Step 5: Convert JSON to CSV
Now, let’s loop through the JSON data and write it to the CSV file using Python.
for item in data:
# Replace 'fieldnames' with your column names
row = [item["field1"], item["field2"], item["field3"]]
csv_writer.writerow(row)
Step 6: Close Files
Don’t forget to close both the JSON and CSV files when finished.
json_file.close()
csv_file.close()
Using Pandas for JSON to CSV Conversion
If you prefer a more convenient way to convert JSON to CSV in Python, you can utilize the pandas library.
import pandas as pd
# Replace "input.json" with your JSON file
data = pd.read_json("input.json")
data.to_csv("output.csv", index=False)
Complete Python Script for JSON to CSV
Here’s the full Python script that combines all the steps, including the use of pandas:
import json
import csv
import pandas as pd
with open("input.json", "r") as json_file:
data = json.load(json_file)
csv_file = open("output.csv", "w", newline="")
csv_writer = csv.writer(csv_file)
fieldnames = ["column1", "column2", "column3"]
csv_writer.writerow(fieldnames)
for item in data:
row = [item["field1"], item["field2"], item["field3"]]
csv_writer.writerow(row)
json_file.close()
csv_file.close()
Conclusion
You’ve successfully converted JSON data into a CSV file using Python. This step-by-step tutorial covers various approaches, including using pandas for a more efficient conversion process.
Whether you’re looking for information on Python JSON to CSV conversion or converting JSON to CSV in Python using pandas, this tutorial has you covered. Feel free to adapt the code to suit your specific needs.
Happy coding! If you want to get online Python tutoring, contact us now to start learning Python.