Try it yourself with our free Json To Csv tool — runs entirely in your browser, no signup needed.

How to Convert JSON to CSV in Python

How to Convert JSON to CSV in Python

Converting JSON to CSV is a common task in data processing and analysis. JSON (JavaScript Object Notation) is a lightweight data interchange format, while CSV (Comma Separated Values) is a widely-used format for tabular data. In this article, we'll explore how to convert JSON to CSV in Python, covering the basics, handling edge cases, and providing performance tips.

Quick Example

Here's a minimal example to get you started:

import json
import csv

# Sample JSON data
json_data = '''
{
    "name": "John",
    "age": 30,
    "city": "New York"
}
'''

# Load JSON data
data = json.loads(json_data)

# Define CSV headers
headers = ['name', 'age', 'city']

# Write to CSV file
with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=headers)
    writer.writeheader()
    writer.writerow(data)

This code reads JSON data, loads it into a Python dictionary, and writes it to a CSV file using the csv module.

Step-by-Step Breakdown

Let's walk through the code:

  1. import json and import csv: We import the json and csv modules, which provide functions for working with JSON and CSV data, respectively.
  2. json_data = '''...''': We define a sample JSON string, which we'll use as input data.
  3. data = json.loads(json_data): We use the json.loads() function to parse the JSON string into a Python dictionary.
  4. headers = ['name', 'age', 'city']: We define the CSV headers, which correspond to the keys in our JSON data.
  5. with open('output.csv', 'w', newline='') as csvfile:: We open a file named output.csv in write mode ('w') and specify the newline='' parameter to avoid issues with newline characters on Windows.
  6. writer = csv.DictWriter(csvfile, fieldnames=headers): We create a DictWriter object, which allows us to write dictionaries to the CSV file. We pass in the fieldnames parameter to specify the headers.
  7. writer.writeheader(): We write the CSV headers to the file.
  8. writer.writerow(data): We write the JSON data to the file as a single row.

Handling Edge Cases

Here are some common edge cases to consider:

Empty/Null Input

If the input JSON data is empty or null, we can add a simple check:

if not data:
    print("Input data is empty or null")
    exit(1)

Invalid Input

If the input JSON data is invalid, json.loads() will raise a JSONDecodeError. We can catch this exception and handle it accordingly:

try:
    data = json.loads(json_data)
except json.JSONDecodeError as e:
    print(f"Invalid JSON data: {e}")
    exit(1)

Large Input

When working with large JSON files, we may encounter memory issues. One solution is to use a streaming JSON parser like json.JSONDecoder:

import json

decoder = json.JSONDecoder()
with open('large_json_file.json', 'r') as f:
    for chunk in f:
        data = decoder.decode(chunk)
        # Process the data in chunks

Unicode/Special Characters

When working with Unicode or special characters, we need to ensure that our CSV writer can handle them correctly. We can use the csv module's unicodecsv module (available in Python 2.x) or the chardet library (available in Python 3.x) to detect the encoding:

import chardet

with open('input.csv', 'rb') as f:
    result = chardet.detect(f.read())
    encoding = result['encoding']
    with open('input.csv', 'r', encoding=encoding) as f:
        reader = csv.reader(f)
        # Process the data

Common Mistakes

Here are three common mistakes developers make when converting JSON to CSV in Python:

Mistake 1: Not Handling Edge Cases

Wrong code:

data = json.loads(json_data)
with open('output.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(data)

Corrected code:

try:
    data = json.loads(json_data)
except json.JSONDecodeError as e:
    print(f"Invalid JSON data: {e}")
    exit(1)

if not data:
    print("Input data is empty or null")
    exit(1)

with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=headers)
    writer.writeheader()
    writer.writerow(data)

Mistake 2: Not Specifying CSV Headers

Wrong code:

data = json.loads(json_data)
with open('output.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(data)

Corrected code:

data = json.loads(json_data)
headers = ['name', 'age', 'city']
with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=headers)
    writer.writeheader()
    writer.writerow(data)

Mistake 3: Not Handling Unicode Characters

Wrong code:

data = json.loads(json_data)
with open('output.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(data)

Corrected code:

import chardet

data = json.loads(json_data)
with open('output.csv', 'w', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(data)

Performance Tips

Here are two performance tips for converting JSON to CSV in Python:

  1. Use a streaming JSON parser: When working with large JSON files, use a streaming JSON parser like json.JSONDecoder to avoid loading the entire file into memory.
  2. Use a buffered writer: When writing to a CSV file, use a buffered writer like csv.writer with a large buffer size to reduce the number of disk writes.

FAQ

Q: What is the difference between json.loads() and json.load()?

A: json.loads() parses a JSON string, while json.load() reads a JSON file.

Q: How do I handle nested JSON data?

A: You can use the json module's loads() function to parse the nested JSON data, and then access the nested data using dictionary keys.

Q: Can I use this code to convert JSON to CSV in Python 2.x?

A: Yes, the code is compatible with Python 2.x, but you may need to use the unicodecsv module instead of the csv module.

Q: How do I handle CSV headers with special characters?

A: You can use the csv module's writer object with the quotechar parameter set to a special character, such as a double quote (") or a single quote (').

Q: Can I use this code to convert JSON to CSV in a web application?

A: Yes, the code can be used in a web application, but you may need to modify it to handle web-specific requirements, such as handling requests and responses.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp