Two shapes for tabular data
CSV (comma-separated values) stores rows of fields as plain text. The first row often holds column names; each following row holds one record. JSON stores structured data with explicit nesting: arrays, objects, strings, numbers, booleans, and null.
Teams convert CSV → JSON to feed web APIs, JavaScript apps, and document databases. They convert JSON → CSV to open data in spreadsheets and BI tools. Neither format replaces the other; they optimize for different editors and parsers.
Practice converting a small table with the CSV to JSON tool.
CSV essentials
A minimal file:
name,age,city
Ada,36,London
Lin,29,"New York"
Rules that matter in practice:
- Fields containing commas, quotes, or newlines must be quoted.
- Quotes inside quoted fields are typically doubled (
""). - The delimiter might be a semicolon or tab in some locales—despite the “C” in CSV.
- There is no single universal standard; RFC 4180 is a common reference, but exporters vary.
CSV has no native nested objects. A column might hold a JSON string as text, but that is a convention layered on top.
JSON essentials for tables
A common target for CSV conversion is an array of objects:
[
{ "name": "Ada", "age": "36", "city": "London" },
{ "name": "Lin", "age": "29", "city": "New York" }
]
Alternatively, an object of arrays (column-oriented) appears in some analytics contexts. Agree on the shape before integrating systems.
JSON can nest: an address object with street and postal code. Flat CSV needs either multiple columns (address.street) or a serialization convention to represent that nesting.
Type inference problems
CSV fields are text. The string 36 might mean a number; true might mean a boolean; 2026-07-18 might mean a date. Converters either:
- Leave everything as strings (safest, least convenient), or
- Guess types (convenient, sometimes wrong—leading zeros in postal codes disappear if cast to numbers).
Postal codes, phone numbers, and IDs should usually stay strings. Document the policy in your pipeline. Round-trip tests catch silent coercion.
Headers and naming
Missing headers force synthetic names (column1, column2) or positional arrays. Duplicate headers create ambiguous keys in JSON objects—some libraries last-write-wins, others error. Clean the header row before conversion: unique, stable, machine-friendly names without spaces if consumers are picky (givenName vs Given Name).
Encoding and newlines
UTF-8 is the right default. Spreadsheet tools on some platforms still export legacy encodings; a weird é sequence usually means UTF-8 bytes were read as Latin-1 (or the reverse). Prefer UTF-8 with an explicit BOM only if a specific consumer requires it.
Newlines inside quoted fields are valid CSV and break naive split('\n') parsers. Use a real CSV parser library.
Practical conversion workflow
- Validate the CSV with a parser (not with ad-hoc splits).
- Confirm delimiter and quote character.
- Decide type policy (all strings vs inferred).
- Emit JSON with consistent key order if diffs matter (optional pretty-print).
- Spot-check row counts and a few known records.
- For the reverse direction, flatten nested JSON deliberately or reject deep structures.
Large files
Browser converters may load entire files into memory. Multi-hundred-megabyte CSVs belong in streaming ETL jobs (command-line tools, databases, Spark-style pipelines). For learning and small admin tasks, in-browser conversion is ideal; for production warehouses, use scalable tooling.
Schema and validation
After CSV → JSON, validate against JSON Schema if you have one: required keys, string formats, numeric ranges. Spreadsheets rarely enforce schemas; your API should. Catch empty required fields early.
Nulls and empty strings
Is an empty CSV cell "", null, or omitted? Different converters choose differently. Align with your API’s semantics. In JSON, null is explicit; omitting a key is different from setting it to null for many consumers.
Nested data strategies
When exporting nested JSON to CSV:
- Flatten with dotted keys (
user.id,user.email). - Serialize nested objects as JSON strings in a cell (awkward in Excel).
- Normalize into multiple CSV files linked by IDs (relational style).
Pick one strategy per pipeline and stick to it.
Security notes
CSV injection in spreadsheets can occur when fields begin with =, +, -, or @ and a spreadsheet interprets them as formulas. When exporting untrusted data for Excel users, sanitize leading characters. JSON APIs should still treat field values as data, not code.
Testing round trips
A healthy test: start with CSV, convert to JSON, convert back, and compare records with a tolerant comparator (header order, numeric string normalization). Perfect textual equality of files is often too strict because of quoting style differences; compare parsed structures instead.
Tooling tips
- Keep a small golden CSV fixture in version control.
- Prefer libraries over regex for parsing.
- Log rejected rows with line numbers.
- Use csv-to-json for interactive exploration of quoting edge cases before coding.
Summary
CSV shines as a flat, spreadsheet-friendly table; JSON shines as typed, nestable structure for APIs and apps. Conversion is straightforward for clean rectangular data and subtle for types, nesting, delimiters, and encoding. Parse properly, decide type rules consciously, validate after conversion, and reserve streaming tools for large datasets.