CSV to JSON Converter: how it works and how to build one
Turn CSV or TSV into JSON and JSON arrays into CSV. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
The first row is used as the header. Quoted fields, commas inside quotes and line breaks inside quotes are handled. Numbers and true/false can be converted to real types. Going the other way, an array of objects becomes a CSV with one column per key, ready for Excel or Google Sheets.
When to use it
- Importing spreadsheets into an app
- Seeding a database
- Turning API data into a sheet for a manager
How to use it
- Choose CSV → JSON or JSON → CSV.
- Paste the data or open a .csv file. Tabs are detected automatically.
- Download the result as .json or .csv.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/csv-to-json
How it works
CSV looks simple but has rules: fields with commas, quotes or line breaks are wrapped in double quotes, and a quote inside is written twice. A small state machine reads the text one character at a time and tracks whether it is inside quotes.
The first row gives the keys. Each following row becomes an object. Optional type detection turns ’12’ into 12 and ‘true’ into true.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
parseCsv.ts (ts)
export function parseCsv(text: string, delim = ","): string[][] {
const rows: string[][] = [];
let row: string[] = [], field = "", quoted = false;
for (let i = 0; i < text.length; i++) {
const c = text[i];
if (quoted) {
if (c === '"' && text[i + 1] === '"') { field += '"'; i++; }
else if (c === '"') quoted = false;
else field += c;
} else if (c === '"') quoted = true;
else if (c === delim) { row.push(field); field = ""; }
else if (c === "\n" || c === "\r") {
if (c === "\r" && text[i + 1] === "\n") i++;
row.push(field); rows.push(row); row = []; field = "";
} else field += c;
}
if (field || row.length) { row.push(field); rows.push(row); }
return rows;
}
export function csvToJson(text: string) {
const [head, ...rows] = parseCsv(text.trim());
return rows.map((r) => Object.fromEntries(head.map((h, i) => [h, r[i] ?? ""])));
}
Make it production-ready
- Detect the delimiter by counting commas, semicolons and tabs in the first line.
- Write the reverse: quote fields that contain the delimiter, quotes or new lines.
- Stream very large files instead of reading them all at once.
Questions
What delimiter does it use?
It detects commas, semicolons and tabs from the header row.
Can I open the CSV in Excel?
Yes. The output uses commas and quotes fields that need it, which Excel and Sheets both read.
Free, runs in your browser, nothing is uploaded.