JSON to CSV in Python

Converting JSON to CSV in Python can be done using the built-in json and csv modules, or by using the pandas library for more complex data structures. Here's a basic guide for both methods:

Using json and csv Modules

This method is straightforward for simple JSON data.

  1. Read the JSON file: Load the JSON data into a Python object.

  2. Open a CSV file: Prepare a CSV file to write the data into.

  3. Create a CSV writer: Use the csv.writer or csv.DictWriter class to create a writer object.

  4. Write the data: Iterate over the JSON data and write it to the CSV file.

import json
import csv

# Load JSON data
with open('data.json') as json_file:
    data = json.load(json_file)

# Open a CSV file and write the data
with open('data.csv', 'w', newline='') as csv_file:
    writer = csv.writer(csv_file)
    writer.writerow(['key1', 'key2', 'key3'])  # Header row, if needed
    for item in data:
        writer.writerow([item['key1'], item['key2'], item['key3']])

Using pandas

For more complex data structures, such as nested JSON, pandas can be very helpful.

  1. Read the JSON file: Use pandas.read_json() to load the JSON data into a DataFrame.

  2. Convert to CSV: Use the DataFrame.to_csv() method to write the data to a CSV file.

import pandas as pd

# Load JSON data into a DataFrame
df = pd.read_json('data.json')

# Convert DataFrame to CSV
df.to_csv('data.csv', index=False)  # Set index=False to exclude row indices

Handling Nested JSON

If your JSON data is nested, you might need to flatten it before converting it to CSV. With pandas, you can use json_normalize:

from pandas import json_normalize
import json

# Load and normalize nested JSON data
with open('nested_data.json') as json_file:
    data = json.load(json_file)
    flat_data = json_normalize(data)

# Convert flattened data to CSV
flat_data.to_csv('flat_data.csv', index=False)

Choose the method that best fits your data structure and requirements. For very complex JSON structures, you might need to preprocess the data to fit into a tabular CSV format.