CSV is the lingua franca of data exports — every spreadsheet, database, and SaaS tool can produce one. JSON is the lingua franca of APIs and modern apps. Sooner or later, every developer needs to bridge the two. The conversion looks trivial until you hit your first quoted comma, escaped newline, or Excel-mangled date. Here's the practical guide.
Why Convert CSV to JSON?
CSV is great for tabular data with a single, flat structure: rows of records, columns of values. But the moment you need to feed that data into:
- A REST API
- A JavaScript front-end
- A NoSQL database (MongoDB, DynamoDB, Firestore)
- A modern data tool (Postman, Zapier, n8n)
- A jq or jsonpath query
…you need JSON. JSON's nested structure also lets you group related fields (address: { street, city, zip }) instead of forcing them flat across many columns.
What CSV-to-JSON Conversion Looks Like
A simple CSV:
id,name,email,signup_date
1,Alice,alice@example.com,2025-01-15
2,Bob,bob@example.com,2025-02-03
3,Carla,carla@example.com,2025-02-12
Converted to JSON, becomes:
[
{ "id": "1", "name": "Alice", "email": "alice@example.com", "signup_date": "2025-01-15" },
{ "id": "2", "name": "Bob", "email": "bob@example.com", "signup_date": "2025-02-03" },
{ "id": "3", "name": "Carla", "email": "carla@example.com", "signup_date": "2025-02-12" }
]
The first row of CSV becomes the keys of each JSON object, and each subsequent row becomes a value object.
The Edge Cases That Bite
CSV looks simple but has a long list of gotchas:
1. Quoted fields with commas.
id,address
1,"123 Main St, Apt 4"
A naive split on commas would produce three fields. The quotes mean "treat this whole thing as one value."
2. Quotes inside quoted fields.
id,quote
1,"She said ""hello"""
Inside a quoted field, two double quotes ("") represent one literal quote.
3. Newlines inside fields.
id,bio
1,"Software engineer.
Likes hiking."
A quoted field can contain literal newlines. Splitting on \n would break the record.
4. Different separators. "CSV" sometimes means TSV (tab-separated), or semicolon-separated (common in European Excel exports), or pipe-separated.
5. Encoding. Most CSVs are UTF-8, but Excel still loves UTF-16 with a BOM, or Windows-1252. Wrong encoding = mojibake.
6. Type ambiguity. Everything in CSV is a string. JSON has numbers, booleans, and null. Should "42" become 42? Should "true" become true? Should "" become "" or null?
A real CSV parser handles all six cases. A string.split(',') in your code handles none of them.
Type Conversion Conventions
When converting to JSON, you'll need to decide how to handle types. The common conventions:
| CSV Value | Default JSON | Strict JSON |
|---|---|---|
42 |
42 (number) |
"42" (string) |
3.14 |
3.14 (number) |
"3.14" (string) |
true / false |
true / false (boolean) |
"true" / "false" (string) |
null / empty |
null |
"" |
"01234" |
"01234" (preserve as string) |
1234 (parse as number — loses leading zero) |
The "strict" approach treats every value as a string — safer for round-tripping data, but inconvenient downstream. The "default" approach guesses types — convenient, but breaks ZIP codes and ID numbers with leading zeros. Pick whichever matches your destination.
How to Convert CSV to JSON Online
Use DevZone's JSON CSV Converter to convert in either direction:
- Paste your CSV (or upload the file).
- Configure the delimiter (comma, tab, semicolon) and whether the first row is a header.
- Choose how to handle types (auto-parse numbers/booleans, or keep everything as strings).
- Copy the resulting JSON, or download as a
.jsonfile.
It runs entirely in your browser, so your data isn't uploaded.
Converting in Code
Python (csv + json):
import csv, json
with open("data.csv") as f:
rows = list(csv.DictReader(f))
with open("data.json", "w") as f:
json.dump(rows, f, indent=2)
csv.DictReader handles quoted fields, escaped quotes, and embedded newlines correctly. Don't roll your own.
Node.js (csv-parse):
import { parse } from "csv-parse/sync";
import { readFileSync, writeFileSync } from "fs";
const csv = readFileSync("data.csv", "utf-8");
const rows = parse(csv, { columns: true, cast: true });
writeFileSync("data.json", JSON.stringify(rows, null, 2));
The cast: true option auto-converts numbers and booleans.
Pandas (when CSV is large):
import pandas as pd
df = pd.read_csv("data.csv")
df.to_json("data.json", orient="records", indent=2)
orient="records" produces an array of objects, the format APIs typically expect. For multi-gigabyte files, use chunksize and stream-write each chunk.
jq (one-liner for quick conversions):
csvtojson < data.csv > data.json
csvtojson is an npm CLI that handles the standard CSV cases.
Nested JSON From Flat CSV
CSV is flat, but JSON usually wants nesting. There's no automatic conversion — you encode the nesting in the column names:
id,name,address.street,address.city,address.zip
1,Alice,123 Main St,Brooklyn,11201
Then post-process to fold dotted keys into nested objects:
const nested = rows.map(row => {
const result = {};
for (const [key, value] of Object.entries(row)) {
const parts = key.split(".");
let cursor = result;
for (let i = 0; i < parts.length - 1; i++) {
cursor[parts[i]] = cursor[parts[i]] ?? {};
cursor = cursor[parts[i]];
}
cursor[parts.at(-1)] = value;
}
return result;
});
This produces:
{
"id": "1",
"name": "Alice",
"address": { "street": "123 Main St", "city": "Brooklyn", "zip": "11201" }
}
FAQ
Can I convert JSON back to CSV?
Yes — but only if the JSON is an array of flat objects. Nested objects need to be flattened first (using the dotted-key convention above), and arrays inside records can't fit cleanly into CSV without some convention like JSON-encoded cells.
What about TSV (tab-separated values)?
TSV is just CSV with \t as the delimiter. The same parsers handle both — set the delimiter option appropriately. TSV has fewer quoting headaches because tabs are rare in real data.
Why do my numbers turn into scientific notation?
Excel does this automatically when it sees large numeric IDs (e.g., 1234567890123 becomes 1.23457E+12). Once Excel has saved that representation, you've lost precision. To prevent it, prefix the cell with a tab or apostrophe, or open the CSV with a tool that doesn't auto-convert (Google Sheets with explicit format, or any text editor).
What's the difference between CSV and "CSV with header"?
A CSV has data rows. A CSV "with header" has an extra first row containing column names. JSON conversion needs the header — without it, the keys would be 0, 1, 2. If your CSV has no header, you'll need to provide one manually before conversion.
Why does my conversion fail on the first row?
A common cause is a UTF-8 BOM () at the start of the file. Excel adds it. The parser sees id as the first column name instead of id. Strip the BOM with a UTF-8 (no BOM) save, or use a parser with BOM-stripping enabled.