CSV Format: A Developer's Complete Guide
Everything you need to know about CSV — the spec, common pitfalls, converting to JSON, and when to choose it over other formats.
What is CSV?
CSV (Comma-Separated Values) is a plain-text format for tabular data. Each line represents a row, and fields within a row are separated by a delimiter — usually a comma. Despite its name, semicolons, tabs, and pipes are all common alternatives depending on locale and tooling.
CSV has no official standard, but RFC 4180 is the closest thing to a spec and is widely referenced. Most modern parsers follow it with varying degrees of strictness.
The RFC 4180 Rules
- Each record is on its own line, terminated by CRLF (
\r\n) - The last record may or may not have a trailing line break
- An optional header row appears as the first record
- Fields may be enclosed in double quotes
- A field containing commas, double quotes, or newlines must be enclosed in double quotes
- A double quote inside a quoted field is escaped by doubling it:
""
Valid CSV Example
id,name,notes
1,Alice,"Works in ""New York"""
2,Bob,"Line 1
Line 2"
3,Charlie,No quotes neededCommon Delimiters
The comma is the default, but real-world CSV often uses other delimiters:
- Semicolon (
;) — Default in European Excel exports where commas are decimal separators - Tab (
\t) — Called TSV (Tab-Separated Values); preferred for data that often contains commas - Pipe (
|) — Used in logs, database dumps, and legacy systems
Always detect the delimiter programmatically when parsing unknown files — count occurrences of each candidate in the first line to make an educated guess.
CSV vs JSON
Both formats are ubiquitous, but they serve different purposes:
When to use CSV
- Exporting data to Excel, Google Sheets, or other spreadsheet tools
- Database imports and exports (
COPYin PostgreSQL,LOAD DATAin MySQL) - Large flat datasets where file size matters (CSV has no key repetition overhead)
- Sharing data with non-developers who will open it in a spreadsheet
When to use JSON
- Nested or hierarchical data (CSV cannot represent trees natively)
- REST APIs and web applications
- When type information matters (JSON preserves numbers, booleans, null)
- Configuration files and structured documents
Converting CSV to JSON
The basic algorithm for converting CSV with a header row to a JSON array of objects:
// 1. Parse CSV into rows (array of arrays)
const rows = parseCSV(csvText);
// 2. First row becomes the keys
const headers = rows[0];
// 3. Remaining rows become objects
const data = rows.slice(1).map(row =>
Object.fromEntries(headers.map((h, i) => [h, row[i] ?? '']))
);The tricky part is the parser itself — a naive split(',') breaks on quoted fields. A robust parser must track whether it is inside a quoted field character by character.
Common Pitfalls
- BOM characters — Excel adds a UTF-8 BOM (
\uFEFF) that corrupts the first header name. Strip it before parsing. - Windows line endings —
\r\nvs\n. Normalize before splitting by lines. - Trailing newline — A final empty line creates a phantom empty row. Drop it after parsing.
- Type coercion — CSV is always strings.
"123"is not the number123. Parse types explicitly after conversion. - Inconsistent column counts — Some rows may have fewer fields than the header. Treat missing fields as empty strings.
Performance Tips for Large Files
- Stream the file rather than reading it all into memory
- In the browser, use
FileReader.readAsText()for upload and parse in chunks - For server-side Node.js, use a streaming CSV library (e.g.,
csv-parsein stream mode) - Paginate the rendered table — rendering 100k rows at once in the DOM will freeze the browser
Convert CSV in your browser
Use our free tools to convert CSV ↔ JSON, view CSV as a table, and parse TSV from Excel — all client-side.
CSV to JSON →