JSON to CSV API: The Complete Guide

Need to convert JSON to CSV in your application? This guide covers everything you need to know about JSON to CSV conversion via API, including handling nested objects, column ordering, and implementation examples.

Why Convert JSON to CSV?

CSV is universally supported by spreadsheet applications like Excel and Google Sheets. Converting JSON to CSV enables easy data export, reporting, and sharing with non-technical users who prefer working with spreadsheets.

Conversion Options

JSON to CSV conversion offers several options:

Flattening Nested Objects

Nested JSON objects can be flattened using dot notation (e.g., user.name becomes a column).

Array Handling

Arrays can be joined into a single cell or expanded into multiple columns.

Column Order

Specify the order of columns or use the order from the first JSON object.

Note: Our API automatically flattens nested objects and handles arrays intelligently.

Using the JSON to CSV API

TinyFn provides a simple endpoint to convert JSON to CSV:

API Request
POST https://api.tinyfn.io/v1/convert/json-to-csv
Headers: X-API-Key: your-api-key
Content-Type: application/json

{
  "json": [
    {"name": "John", "age": 30, "city": "New York"},
    {"name": "Jane", "age": 25, "city": "Boston"}
  ]
}
Response
{
  "csv": "name,age,city\nJohn,30,New York\nJane,25,Boston",
  "rows": 2,
  "columns": 3
}

Parameters

Parameter Type Description
json array Array of JSON objects to convert (required)
delimiter string Field delimiter (default: ,)
include_header boolean Include header row (default: true)
columns array Specify column order (optional)

Code Examples

JavaScript / Node.js

const data = [
  {name: "John", age: 30},
  {name: "Jane", age: 25}
];
const response = await fetch('https://api.tinyfn.io/v1/convert/json-to-csv', {
  method: 'POST',
  headers: {
    'X-API-Key': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ json: data })
});
const result = await response.json();
console.log(result.csv); // "name,age\nJohn,30\nJane,25"

Python

import requests

data = [
    {"name": "John", "age": 30},
    {"name": "Jane", "age": 25}
]
response = requests.post(
    'https://api.tinyfn.io/v1/convert/json-to-csv',
    json={'json': data},
    headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['csv'])  # "name,age\nJohn,30\nJane,25"

cURL

curl -X POST "https://api.tinyfn.io/v1/convert/json-to-csv" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"json": [{"name": "John", "age": 30}]}'

Common Use Cases

  • Data Export: Export application data for spreadsheet analysis
  • Reports: Generate CSV reports from API data
  • Data Migration: Prepare data for import into other systems
  • User Downloads: Let users download their data as CSV
  • Backup: Create portable backups of JSON data

Best Practices

  1. Flatten before converting: Pre-flatten complex nested structures
  2. Handle special characters: Quote fields containing commas or newlines
  3. Consistent schema: Ensure all objects have the same properties
  4. Large datasets: Use streaming for very large JSON arrays

Use via MCP

Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:

{
  "mcpServers": {
    "tinyfn-convert": {
      "url": "https://api.tinyfn.io/mcp/convert/",
      "headers": {
        "X-API-Key": "your-api-key"
      }
    }
  }
}

See all conversion tools available via MCP in our Conversion MCP Tools for AI Agents guide.

Try the JSON to CSV API

Get your free API key and start converting data in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key