Explain Codes LogoExplain Codes Logo

How can I convert JSON to CSV?

python
json
pandas
csv
Nikita BarsukovbyNikita Barsukov·Feb 9, 2025
TLDR

To immediately convert JSON into CSV in Python, tap into the powerful pandas library. Two simple steps: read the JSON data using pd.read_json() and export it to CSV in a flash with df.to_csv(index=False). Exactly like this:

import pandas as pd df = pd.read_json('path_to_your_json_file') df.to_csv('path_to_output_csv_file', index=False)

Swap 'path_to_your_json_file' and 'path_to_output_csv_file' with your actual file paths, to read from JSON and export the CSV, sans indices.

Handling complex JSON

Sometimes, your data may be hidden behind several layers of nested JSON. This isn't ideal because CSV is a linear, tabular format. In such cases, you need to flatten the JSON file before converting. Let's use Python's json_normalize function:

from pandas import json_normalize import json with open('path_to_nested_json') as json_file: data = json.load(json_file) flat_data = json_normalize(data, 'field_to_be_flattened') # flatten like a panini press flat_data.to_csv('path_to_new_csv', index=False) # Voila! CSV file is served!

It's important to replace 'field_to_be_flattened' with the actual nested field you’re aiming to flatten in your JSON.

Managing edge cases and errors

Conversion from JSON to CSV is straightforward until it's not. For instance, data not formatted as a list of dictionaries can trigger the infamous "sequence expected" errors. Additionally, some nested structures can't possibly fit into the two-dimensional realm of CSV. It's like asking a multi-layered lasagna to morph into flatbread. Not gonna happen.

Data integrity is crucial. Therefore, when dealing with JSON files, ensure the correct character encoding to maintain the fidelity of your data. Supplementary, pathlib can help with managing file paths more robustly.

Necessary data checks

Here are some Python-ic principles to live by:

  • Use try-except blocks to tackle exceptions during the file reading and writing.
  • Always validate the JSON file's encoding before conversion. Misinterpretation of characters can hurt, especially with international data.
  • Review the JSON to ensure that its structure is compatible with flat CSV format. As they say, flat earth theory doesn't always apply, especially with JSON.

CSV Conversion Without Pandas

If you're not a pandas-fan, or in case of small datasets, basic Python's csv and json libraries will do it with elegance and style. Here:

import json import csv # Read JSON file with open('json_filepath') as file: json_data = json.load(file) # Create CSV file with open('csv_filepath', mode='w') as file: writer = csv.DictWriter(file, fieldnames=json_data[0].keys()) # Get ready for some magic writer.writeheader() # Initiate the spell writer.writerows(json_data) # And... Abracadabra!

Don't forget to replace 'json_filepath' and 'csv_filepath' with your actual file paths!