How do you convert JSON to CSV using Python quickly and easily?
If you’re looking to store data received from an API that responds with JSON data and want to convert this to a CSV file then Python can easily help.
Simply import the standard
json
and
csv
libraries into your Python code then read the json data into a variable and construct it to a one-dimensional format that can make it easy to store the data into a csv file.
For example, suppose I have the following data in JSON format:
{
"employees": [{
"name": "John Smith",
"salary": 120000
},{
"name": "Jane Doe",
"salary": 125000
},{
"name": "Mark Twain",
"salary": 90000
}]
}
In the sample JSON response of employee data there contains an
employees
property which contains an array of staff names with their corresponding annual salary.
And the objective is to convert this JSON to a CSV file as follows:
Employee Name,Annual Salary
John Smith,120000
Jane Doe,125000
Mark Twain,90000
Here’s a step by step way of how you can perform this operation.
Importing Required Libraries
Before we can start converting JSON to CSV in Python, your code needs to import two standard libraries:
json
and
csv
.
As these two libraries are built-in to Python there is
no need
to install them using the
pip
package manager.
To use these two libraries in your code write the following at the top of your Python file:
import csv
import json
Get JSON Data
The first step in converting JSON to CSV in Python is to put the JSON data into a variable. Depending on how the JSON data is received, you may need to perform a few things.
For example, if your JSON data is in a file, then you would need to open the file first to store the contents in a variable.
Here are the steps involved:
Opening JSON file
To open a JSON file, you need to use the
open()
function in Python. This function returns a file object, which you can use to read the contents of the file.
Here is an example:
with open('example.json', 'r') as f:
json_data = json.load(f)
In this example, I am opening a file named
example.json
and reading its contents into a variable named
data
. The
json.load()
function takes a text or binary file as its first parameter.
If you have other data types containing the
json
data, such as a
str
, you would use the
json.loads()
function instead. The way I remember which function to use is to think that the
loads
function is
loading a
s
tring
.
You use the
with
statement to ensure that the file is closed properly after you are done reading from it.
After following these substeps, you should now have the JSON data stored in a Python object, which can be used to perform the conversion to CSV.
Converting JSON to CSV
To convert JSON to CSV format, you will need to format your data in a way that is suitable for CSV. CSV stands for “comma-separated values” and is essentially a text file with rows of record data separated by commas or semicolons.
Get CSV Data
First, you need to create an empty list to store our data in CSV format. You will loop through your JSON data and create a dictionary for each record. For each key-value pair in our JSON record, you will add the value to a list and then join all of the values in the list with a comma separator.
Here’s an example:
csv_data = []
csv_header = ['Employee Name', 'Annual Salary']
csv_data.append(csv_header)
# loop through the json_data variable
for employee in json_data['employees']:
csv_row = [employee['name'], employee['salary']]
csv_data.append(csv_row)
In the above code I have created a variable labelled
csv_data
which will store all the data I want exported to a CSV file. It will be a two-dimensional list with the first row being the
csv_header
.
To obtain the data I need I then loop through the
json_data['employees']
property and fetch the property from each dict and append this to my two-dimensional
csv_data
variable.
Export CSV
Now that the data is in the correct format for CSV, you can use Python’s built-in CSV module to write the data to a CSV file. Here’s an example:
import csv
with open('output.csv', 'w') as f:
csv_writer = csv.writer(csv_data)
csv_writer.writerows(csv_data)
This code snippet creates a new CSV file called
"output.csv"
and writes the contents of
csv_data
list to the file.
If you have a preference for other delimiters such as a semi-colon you can add the parameter
delimiter=";"
to the
csv.writer()
function.
The resulting CSV file should now be ready for use.
Here is the full code:
Handling errors
When working with data, it’s important to be prepared for things to go wrong. Here are some common errors you may encounter while converting JSON to CSV and how to handle them:
Attribute or key error
One common error you may encounter is an attribute or key error, which means the script cannot find a key or attribute in your data. Double-check that the key or attribute exists and make sure it’s spelled correctly.
Value error
A value error occurs when the script cannot convert a particular value from one type to another. For example, if you have a string that should be an integer, but it contains non-numeric characters, you’ll get a value error. Be sure to validate your data before trying to convert it.
Encoding error
If your data contains non-ASCII characters, you may encounter an encoding error. You can fix this by encoding your data with a different encoding format, such as UTF-8 or ASCII.
File not found error
If your script cannot find the JSON or CSV file you’re trying to read or write to, you’ll get a file not found error. Double-check that the file path is correct and that the file actually exists.
Memory error
If you’re working with very large datasets, you may encounter a memory error when trying to convert JSON to CSV. To prevent this, you can split your data into smaller chunks or use a more efficient method of conversion.
By anticipating these common errors and knowing how to handle them, you can ensure a successful JSON to CSV conversion in Python.
Converting JSON to CSV Data: Summary
Converting JSON to CSV is a trivial task in Python. Using the two built-in libraries
json
and
csv
you can perform the task of producing a CSV file fairly easily.
The steps necessary to convert JSON data into CSV are as follows:
- Get your JSON data into a variable.
- Loop through your JSON data and extract the data into a list of dictionaries or list of lists (the example in this article was a list of lists).
- Output the csv data to a file.
As you can see the steps are quite simple and can be easily performed using Python.