65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
JSON to CSV Converter - A simple utility to convert JSON files to CSV format.
|
|
Usage: python3 json_to_csv.py <input.json> <output.csv>
|
|
"""
|
|
|
|
import json
|
|
import csv
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def flatten_dict(d, parent_key='', sep='_'):
|
|
"""Flatten nested dictionary for CSV compatibility."""
|
|
items = []
|
|
for k, v in d.items():
|
|
new_key = f"{parent_key}{sep}{k}" if parent_key else k
|
|
if isinstance(v, dict):
|
|
items.extend(flatten_dict(v, new_key, sep).items())
|
|
elif isinstance(v, list):
|
|
items.append((new_key, json.dumps(v)))
|
|
else:
|
|
items.append((new_key, v))
|
|
return dict(items)
|
|
|
|
|
|
def convert_json_to_csv(json_path, csv_path):
|
|
"""Convert JSON file to CSV."""
|
|
with open(json_path, 'r') as f:
|
|
data = json.load(f)
|
|
|
|
if isinstance(data, list):
|
|
records = [flatten_dict(record) for record in data] if data else []
|
|
elif isinstance(data, dict):
|
|
records = [flatten_dict(data)]
|
|
else:
|
|
raise ValueError("JSON must contain objects or array of objects")
|
|
|
|
if not records:
|
|
with open(csv_path, 'w') as f:
|
|
pass
|
|
return
|
|
|
|
fieldnames = sorted(set(key for record in records for key in record.keys()))
|
|
|
|
with open(csv_path, 'w', newline='') as f:
|
|
writer = csv.DictWriter(f, fieldnames=fieldnames)
|
|
writer.writeheader()
|
|
writer.writerows(records)
|
|
|
|
return {
|
|
'input_path': str(json_path),
|
|
'output_path': str(csv_path),
|
|
'records_processed': len(records),
|
|
'columns': fieldnames
|
|
}
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) != 3:
|
|
print("Usage: python3 json_to_csv.py <input.json> <output.csv>")
|
|
sys.exit(1)
|
|
|
|
result = convert_json_to_csv(sys.argv[1], sys.argv[2])
|
|
print(json.dumps(result, indent=2)) |