Need to convert CSV data to JSON in your application? This guide covers everything you need to know about CSV to JSON conversion via API, including header handling, type inference, and implementation examples.
Why Convert CSV to JSON?
CSV (Comma-Separated Values) is a common format for spreadsheet data and data exports. Converting to JSON makes the data easier to work with in web applications, APIs, and databases that expect structured JSON data.
Conversion Options
CSV to JSON conversion offers several options:
Header Row
Use the first row as property names for each JSON object, or generate numeric indices.
Type Inference
Automatically detect and convert numbers, booleans, and dates instead of keeping everything as strings.
Delimiter
Support for comma, semicolon, tab, or other delimiters for regional CSV variants.
Using the CSV to JSON API
TinyFn provides a simple endpoint to convert CSV to JSON:
POST https://api.tinyfn.io/v1/convert/csv-to-json
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"csv": "name,age,city\nJohn,30,New York\nJane,25,Boston"
}
{
"json": [
{"name": "John", "age": 30, "city": "New York"},
{"name": "Jane", "age": 25, "city": "Boston"}
],
"rows": 2,
"columns": 3
}
Parameters
| Parameter | Type | Description |
|---|---|---|
csv |
string | CSV data to convert (required) |
has_header |
boolean | First row is header (default: true) |
delimiter |
string | Field delimiter (default: auto-detect) |
infer_types |
boolean | Convert numbers and booleans (default: true) |
Code Examples
JavaScript / Node.js
const csv = "name,age\nJohn,30\nJane,25";
const response = await fetch('https://api.tinyfn.io/v1/convert/csv-to-json', {
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ csv })
});
const result = await response.json();
console.log(result.json); // [{name: "John", age: 30}, ...]
Python
import requests
csv_data = "name,age\nJohn,30\nJane,25"
response = requests.post(
'https://api.tinyfn.io/v1/convert/csv-to-json',
json={'csv': csv_data},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['json']) # [{"name": "John", "age": 30}, ...]
cURL
curl -X POST "https://api.tinyfn.io/v1/convert/csv-to-json" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"csv": "name,age\nJohn,30\nJane,25"}'
Common Use Cases
- Data Import: Import spreadsheet data into web applications
- API Integration: Convert CSV exports for JSON-based APIs
- Database Loading: Prepare CSV data for NoSQL databases
- Data Analysis: Convert for JavaScript data visualization libraries
- ETL Pipelines: Transform data between systems
Best Practices
- Validate CSV structure: Ensure consistent column counts
- Handle quoted fields: CSV fields with commas should be quoted
- Check encoding: Use UTF-8 encoding for international characters
- Test type inference: Verify numbers and booleans convert correctly
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 CSV to JSON API
Get your free API key and start converting data in seconds.
Get Free API Key