Your destination for complete Tech news

How to Convert JSON to CSV in Python (Step-by-Step Guide)

582 0
< 1 min read

Intro:
Sometimes you need to export JSON data into a CSV file for reporting. Python makes this simple using the csv module.

Snippet:

import csv
import json

data = [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30}
]

with open("output.csv", "w", newline="") as file:
    writer = csv.DictWriter(file, fieldnames=["name", "age"])
    writer.writeheader()
    writer.writerows(data)

Takeaway:
The csv.DictWriter is best for structured JSON → CSV conversions.