Skip to main content

Overview

The CSV to JSON converter parses CSV (Comma-Separated Values) data into JSON arrays of objects. Features automatic header detection, type inference, and robust parsing that handles quoted fields, escaped characters, and edge cases.

Use Cases

  • Data Import: Import CSV exports from Excel, Google Sheets, or databases
  • API Preparation: Convert CSV datasets for JSON-based APIs
  • Data Processing: Transform tabular data for JavaScript/Python processing
  • Configuration: Convert CSV config files to JSON for applications
  • Testing: Generate JSON test fixtures from CSV data

Input Format

Standard CSV with headers:
id,name,email,active
1,Alice,[email protected],true
2,Bob,[email protected],false
3,Charlie,[email protected],true

With Quoted Fields

name,description,price
"Laptop","High-performance laptop with 16GB RAM",999.99
"Mouse","Wireless mouse, ergonomic design",24.99

Tab-Delimited

id	name	email
1	Alice	[email protected]
2	Bob	[email protected]

Output Format

JSON array of objects with type conversion:
[
  {
    "id": 1,
    "name": "Alice",
    "email": "[email protected]",
    "active": true
  },
  {
    "id": 2,
    "name": "Bob",
    "email": "[email protected]",
    "active": false
  },
  {
    "id": 3,
    "name": "Charlie",
    "email": "[email protected]",
    "active": true
  }
]

Examples

sku,product,price,inStock
LP-001,Laptop,999.99,true
MS-002,Mouse,24.99,true
KB-003,Keyboard,74.99,false
name,description,tags
"Project A","High priority, backend work","urgent,api,backend"
"Project B","Frontend redesign","ui,frontend"
user_id,score,level,timestamp
1001,9500,15,1678901234
1002,12300,18,1678901456
1003,7800,12,1678901678

Features

  • Automatic Header Detection: First row treated as column names
  • Type Inference: Automatically converts numbers, booleans, null
  • Quoted Field Support: Handles fields with commas, quotes, newlines
  • Tab Support: Detects and parses tab-delimited values
  • Empty Line Handling: Skips empty rows automatically
  • Error Reporting: Clear error messages for malformed CSV

Type Conversion Rules

Numbers

price
99.99
Converts to: {"price": 99.99}

Booleans

active
true
false
Converts to: {"active": true}, {"active": false}

Null

value

null
NULL
Converts to: {"value": null}

Strings

All other values remain strings, including:
  • Numbers with leading zeros: "001"
  • Dates: "2024-01-15"
  • Mixed content: "abc123"

Implementation Details

From lib/tools/engine.ts:606-617:
case 'csv-to-json': {
  const { parse: csvParse } = await import('papaparse');
  const parsed = csvParse<Record<string, unknown>>(input, {
    header: true,
    skipEmptyLines: true,
    dynamicTyping: true,
  });
  if (parsed.errors.length) {
    return { output: `CSV parse error: ${parsed.errors[0].message}` };
  }
  return { output: stringify(parsed.data, true) };
}
Uses papaparse with dynamicTyping: true for intelligent type conversion. Numbers, booleans, and null values are automatically detected and converted.
If type conversion causes issues (e.g., leading zeros being dropped), disable dynamic typing by using a CSV parser with dynamicTyping: false in your own code.

Build docs developers (and LLMs) love