How to convert CSV to JSON
Short answer
To convert CSV to JSON, use the first row as the keys and each following row as one object. Each cell becomes a value on its row's object, and the result is an array of those objects.
The basic mapping
CSV (comma-separated values) is a flat table: the first row holds the column names, and every other row is a record. JSON represents the same data as an array of objects, where each object's keys are the column names.
name,role,active
Ada,author,true
Grace,engineer,true[
{ "name": "Ada", "role": "author", "active": "true" },
{ "name": "Grace", "role": "engineer", "active": "true" }
]Quotes, commas, and newlines
A cell can legitimately contain a comma, a quote, or a line break if it is wrapped in double quotes, per the CSV convention in RFC 4180. A naive split on commas breaks on this, which is why a real parser is safer than a one-line script.
true and 42 arrive as strings. If you need real booleans or numbers, convert them after parsing.Common mistakes
- Splitting on commas without handling quoted fields.
- Duplicate or empty header names, which collide as JSON keys.
- Assuming numbers and booleans are typed; they are text until you convert them.