How can I catch new posts?
Bookmark this blog and watch the homepage and tools hub—we surface new guides there. No account or mailing list is required to read articles.
Learn how to convert JSON data to CSV format using Python with complete code examples, handling nested JSON, and best practices for data processing.
In daily data processing, we often encounter scenarios where we need to convert JSON format data to CSV tables, such as exporting API data, analyzing log files, or importing into Excel for visualization.
This article will show you how to efficiently convert JSON → CSV using Python, with several common implementation approaches.
| Feature | JSON | CSV |
|---|---|---|
| Data Structure | Tree-like, nested | Flat table |
| Readability | Machine and human readable | Human-friendly, simple structure |
| Common Use Cases | Web APIs, configuration files | Excel, database import/export |
| File Extensions | .json | .csv |
When you want to view or analyze JSON data from APIs in spreadsheets, you need to convert it to CSV.
Python comes with powerful json and csv standard libraries that can handle conversion directly without additional dependencies.
import json
import csv
# Read JSON file
with open('data.json', 'r', encoding='utf-8') as f:
data = json.load(f)
# Open CSV file for writing
with open('output.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
# Write headers (extract from first element's keys)
header = data[0].keys()
writer.writerow(header)
# Write each row of data
for row in data:
writer.writerow(row.values())
print("✅ JSON successfully converted to CSV: output.csv")Input Example:[
{"name": "Alice", "age": 25, "city": "Shanghai"},
{"name": "Bob", "age": 30, "city": "Beijing"}
]Output Result (CSV):name,age,city
Alice,25,Shanghai
Bob,30,BeijingIf your JSON contains nested objects or arrays, you need to "flatten" the structure first.
pandas.json_normalize()import pandas as pd
import json
# Read nested JSON
with open('nested.json', 'r', encoding='utf-8') as f:
data = json.load(f)
# Flatten nested structure to flat table
df = pd.json_normalize(data)
# Export to CSV
df.to_csv('output.csv', index=False, encoding='utf-8-sig')
print("✅ Nested JSON successfully converted to CSV: output.csv")Example Input:[
{
"user": {"name": "Alice", "id": 101},
"stats": {"score": 98, "level": 3}
},
{
"user": {"name": "Bob", "id": 102},
"stats": {"score": 87, "level": 2}
}
]Output Result:user.name,user.id,stats.score,stats.level
Alice,101,98,3
Bob,102,87,2✅ Tip: pandas.json_normalize() automatically flattens nested fields, perfect for complex JSON.You can also load JSON data directly from remote APIs:
import requests
import pandas as pd
url = "https://jsonplaceholder.typicode.com/users"
data = requests.get(url).json()
df = pd.json_normalize(data)
df.to_csv('users.csv', index=False, encoding='utf-8-sig')
print("✅ JSON loaded from URL and exported to CSV")→ Use encoding='utf-8-sig' when saving CSV to ensure proper Chinese character display in Excel.
→ If JSON is a single object (like {}), manually wrap it in an array:
data = [data]3️⃣ How to import to Excel?
→ In Excel, click "Data" → "From Text/CSV" → select the output file.
If you need a command-line tool:
# json2csv.py
import sys
import pandas as pd
def json_to_csv(input_file, output_file):
df = pd.read_json(input_file)
df.to_csv(output_file, index=False, encoding='utf-8-sig')
print(f"✅ {input_file} converted to {output_file}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python json2csv.py input.json output.csv")
else:
json_to_csv(sys.argv[1], sys.argv[2])Execute:
python json2csv.py data.json output.csv
| Tool | Use Case | Advantages |
|---|---|---|
csv + json | Simple JSON | Standard library, zero dependencies |
pandas | Nested JSON, large files | Powerful features, batch processing |
requests | Network data | Direct API fetching |
If you only need occasional conversion, you can use our online tool 👉 JsonWork.com/json-to-csv
No Python installation required, supports paste / upload / download, 100% local processing, more secure and convenient.
Dedicated to providing developers with the best JSON processing tools
More posts coming soon...
Back to BlogFollowing the blog, topics we cover, and how to suggest guides.
Bookmark this blog and watch the homepage and tools hub—we surface new guides there. No account or mailing list is required to read articles.
JSON validation, formatting, conversion, debugging workflows, and JSON Work releases—mapped to what the free on-site tools can do locally in your browser.
Yes. Reach out via the About page or GitHub; we prioritize guides tied to real integration and debugging scenarios.