Converting JSON to CSV via API for Data Exports
Flattening JSON for CSV Export
JSON is great for APIs but awkward for spreadsheets. Converting nested JSON to CSV makes data consumable by Excel, Google Sheets, and BI tools without any post-processing.
The Flattening Challenge
Consider a nested structure:
[
{ "id": 1, "user": { "name": "Alice", "email": "[email protected]" }, "score": 99 },
{ "id": 2, "user": { "name": "Bob", "email": "[email protected]" }, "score": 87 }
]
A naive conversion would put the entire user object in one column.
The API flattens nested keys using dot-notation by default:
| id | user.name | user.email | score |
|---|---|---|---|
| 1 | Alice | [email protected] | 99 |
| 2 | Bob | [email protected] | 87 |
API Usage
curl -X POST https://api.toolkitapi.io/v1/convert/json-to-csv \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d '{"data": [{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}'
The response is a plain-text CSV, ready to write to a .csv file.
Key Options
delimiter— default,, use\tfor TSVflatten_depth— limit how deep nested objects are flattened (default: unlimited)null_value— string to use fornullfields (default: empty string)
Handling Arrays Inside Objects
When a field is an array (e.g., tags), the API serialises it as a JSON string in the cell
unless you specify expand_arrays: true, which repeats the parent row for each array element.
Automating Data Exports
import httpx, csv, io
resp = httpx.post(
"https://api.toolkitapi.io/v1/convert/json-to-csv",
headers={"X-API-Key": API_KEY},
json={"data": records, "delimiter": ","},
)
reader = csv.DictReader(io.StringIO(resp.text))
for row in reader:
print(row)
This pattern works well in data pipelines where you fetch JSON from one service and need to write CSV to an S3 bucket or email attachment.