Convert JSON to CSV in Python 3 using Pandas
In this tutorial we'll be converting a JSON file to CSV using Python.
Let's say we have a JSON file that looks like this:
[
{
"id": 1,
"name": "Albert",
"address": {
"city": "Amsterdam"
}
},
{
"id": 2,
"name": "Adam",
"address": {
"city": "Paris"
}
},
{
"id": 3,
"name": "Sara",
"address": {
"city": "Madrid"
}
}
]
Step 1: Install pandas
You could technically use the standard json
and csv
modules from Python to read the JSON file and write a CSV file, but depending on how your JSON file is structured, it can get complicated, and pandas
has some great functions to handle these cases, without mentioning it's really fast, so that's what we will use.
You can install pandas
using pip
running this command:
$ pip install pandas
Step 2: Load the JSON file
Let's load the JSON file using the json
Python module:
import json
with open('input.json', encoding='utf-8') as file:
data = json.loads(file.read())
Step 3: Normalize the JSON data
Because the JSON format can hold structured data like nested objects and arrays, we have to normalize this data so that it can be respresented in the CSV format.
A quick way to do this is to use the pandas.normalize_json
function, which will take JSON data and normalize it into a tabular format, you can read more about this function here.
import pandas
df = pandas.json_normalize(data)
In our case, if we print the resulting dataframe, it will look something like this:
print(df)
id name address.city
0 1 Albert Amsterdam
1 2 Adam Paris
2 3 Sara Madrid
As you can see the address
which was an object, was flattened out to a column.
Step 4: Export to CSV
Now that our data is normalized to a tabular format, let's write our CSV file!
Pandas has a convenient function for this:
df.to_csv('input.csv', index=False, encoding='utf-8')
And here is the resulting CSV file:
id,name,address.city
1,Albert,Amsterdam
2,Adam,Paris
3,Sara,Madrid
If you want to configure the output, check out the documentation page for the to_csv
function here
Putting everything together
Here's the final code to convert JSON to CSV:
import json
import pandas
with open('input.json', encoding='utf-8') as file:
data = json.loads(file.read())
df = pandas.json_normalize(data)
df.to_csv('output.csv', index=False, encoding='utf-8')
Online conversion
If your file is too big or your data is a bit more complex, you can try our online JSON to CSV converter, which can handle huge files and more use cases.