CSV to JSON Converter
Nothing in this document was dropped or changed.
Going the other way? Convert JSON back to CSV →
How do you convert CSV to JSON?
CSV to JSON reads a table and writes a list: the header row becomes the keys, and every later row becomes one object in a JSON array.
- Paste the CSV above. The first line is read as the header row, so it names the columns.
- Choose the delimiter. Comma is the RFC 4180 separator; the semicolon and tab options read a file written with one of those instead.
- Split every later line into fields on that delimiter, honouring the double quotes: a quoted field may hold the delimiter, a line break, and a double quote written twice.
- Pair each field with the name of its column and write one object per record. The result is a JSON array of objects.
This page writes a JSON array of objects, one object per record, with the header row supplying the keys; the delimiter is selectable and a quoted field keeps its embedded commas.
CSV to JSON Conversion Table
Quoting is where a CSV file stops being obvious, so this is the whole of it. RFC 4180 fixes four things for CSV: each record sits on its own line ending in CRLF, fields are separated by commas, a field holding a comma, a double quote or a line break is wrapped in double quotes, and a double quote inside a quoted field is written twice.
| Field as written | What that means | JSON string |
|---|---|---|
| Ada | an unquoted field | "Ada" |
| "Lovelace, Ada" | a quoted field, so the comma belongs to the value | "Lovelace, Ada" |
| "she said ""no""" | a doubled quote inside a quoted field is one quotation mark | "she said \"no\"" |
| " Ada " | spaces are part of the field and are not trimmed | " Ada " |
| "one line then another" | a quoted field may hold a line break | "one line then another" |
| (nothing) | an empty field | "" |
A format is defined by what it can say. The rows below put the two halves of this conversion side by side, so the gap the conversion has to cross is a thing you can read rather than a thing you find out later. Every value in the table is the wording the defining document uses, not a summary of it, and a dash means the format has no answer to that row at all.
| What it says | CSV | JSON |
|---|---|---|
| Specification | RFC 4180, Informational, October 2005 | RFC 8259, Standards Track, December 2017; also ECMA-404 |
| Media type | text/csv | application/json |
| Structure | flat records of fields; no nesting | objects and arrays, nested to any depth |
| Delimiter | a comma (%x2C), fixed by the RFC 4180 grammar | a comma (%x2C) between two members of an object and between two elements of an array |
| Quoting | double quotes around the field; a double quote inside a quoted field is written twice | double quotes; a quotation mark, a reverse solidus and the control characters U+0000 to U+001F must be escaped with a reverse solidus |
| Header line | optional; its presence is signalled by the text/csv header parameter, not by the file | — |
| Type set | none - every field is text | string, number, boolean, null, object, array |
| Nesting | none | unlimited by the grammar; a parser may set its own depth limit |
| Ordering | — | an object is unordered; an array is ordered |
| Record separator | CRLF; the last record may omit it | — |
| Comment | none defined | none - the grammar has no comment production |
| Spaces | spaces are part of a field and are not ignored | — |
| Encoding | — | UTF-8 |
| Status | Informational; RFC 4180 records that no single master specification for the format exists | — |
Worked Example: CSV to JSON
Two records and a header row, converted here at build time by the same code the box above runs. Read the working under it from left to right: where the field sat in the file, what it was, what was decided about it, and what came out.
name,age
Ada,36
Grace,45 [{"name":"Ada","age":"36"},{"name":"Grace","age":"45"}] Result: the CSV above is the JSON beside it, and every decision that made it is listed below.
Every value came out as a JSON string, and that is a decision rather than an accident. RFC 8259 gives JSON four primitive types - string, number, boolean and null - and two structured types, object and array. There is no date type and no separate integer type, so a date arrives as a string and every number is read the same way.
A CSV file states no type at all, so reading one out of a field would be a guess. The type rule above turns that guess on when you want it, and the converter then reports what the guess cost. RFC 8259 requires JSON exchanged between systems to be encoded as UTF-8, and forbids adding a byte order mark to the front of a transmitted JSON text. A parser may ignore a byte order mark it finds rather than treat it as an error.
Check it in reverse
The round trip is exact under the text rule and only under it. Every field went in as text and came out as a JSON string, so writing those strings back into a table gives the characters you started with, quoting included. Turn the type rule to numbers and booleans and that stops being true: a field of 007 becomes the number 7 and the zeros cannot come back.
To prove nothing was lost, convert the JSON result back to CSV and compare it with the file you started from.
What This Conversion Cannot Carry
The JSON below has to be readable by every parser that meets it, and that fixes two things about what this page can write. JSON has no comments and no trailing commas because RFC 8259's grammar has no production for either. A comma may appear only between two members of an object or two elements of an array, so a comma before the closing brace or bracket makes the text invalid.
- An empty cell and an empty string
- An empty field became the empty string. CSV has no null (FMT-csv-types), so an empty cell, a missing value and an empty string are one thing on the page and cannot be told apart on the way back.
- A record with too few fields
- A record carries fewer fields than the header row names, so the keys it has no value for were left out of its object rather than given an invented one. RFC 4180 §2 rule 4 says each line should contain the same number of fields throughout the file.
- A blank line between records
- A blank line stood between two records. RFC 4180 has no production for one, so it was skipped rather than read as a record of one empty field.
- A line ending that is not CRLF
- RFC 4180 fixes the record separator at CRLF and this file uses another line ending. The RFC itself records that implementations do this, so the field split is unaffected — but the output is written with CRLF.
- A byte order mark
- A byte order mark stood at the front of the file. It was removed rather than read as part of the first field.
Turning the type rule on adds one more, and it is the one that surprises people. A JSON number is written in base 10, leading zeros are not allowed, and Infinity and NaN are not permitted. RFC 8259 lets a parser set its own limits on range and precision, so integers outside -(2^53)+1 to (2^53)-1 are where two parsers start to disagree about the same file. One thing here is refused rather than reported: a header row that names the same column twice. RFC 8259 says the names in an object should be unique and records that software receiving a repeated name behaves unpredictably: some implementations keep the last pair, some fail, some keep all of them. An object is unordered, and parsers differ over whether they expose member order at all. Each line above is written by the converter itself and appears under your own result whenever your file raises it.
The sample above reverses exactly - write the same records back out as CSV rows .
What Changes the JSON Output?
The delimiter you choose
The delimiter decides the field split, and nothing else does. A semicolon-separated or tab-separated file is not RFC 4180 CSV. The grammar fixes the separator at the comma (%x2C) and the media type has no delimiter parameter, so any other separator is read by convention rather than by the specification. The three options above cover the files people actually have.
Whether the first line names the columns
A CSV file cannot tell you whether its first line is a header row. RFC 4180 is Informational and says plainly that no single master specification for CSV exists. The header line is optional and is signalled outside the file by the text/csv header parameter, and the document warns that some implementations use line endings other than CRLF.
The encoding the file arrived in
An encoding the reader guesses wrong turns accented characters into the wrong characters. RFC 4180 carries the character set as an optional text/csv parameter and names US-ASCII as common usage. Nothing inside a CSV file records its encoding, so the encoding has to be known before the file is read.
CSV to JSON in Code
The same conversion in two lines, one per language. Both read the header row first and pair each field with its name. Neither line reports what the conversion could not carry; the converter above does.
Python: json.dumps(list(csv.DictReader(open("in.csv", newline="")))) JavaScript: JSON.stringify(rows.map(r => Object.fromEntries(head.map((h, i) => [h, r[i]])))) Common Mistakes
- Splitting the line on every comma. A comma inside a quoted field belongs to the value, and a split on it moves every later field one column left.
- Assuming the file uses commas because it is called a CSV. A European locale writes semicolons, and the field count is the giveaway.
- Trimming the spaces around a field. They are part of the value, so " Ada " and "Ada" are two different keys in the JSON.
Frequently Asked Questions
Can Excel be saved as JSON?
Not directly. Save the sheet as CSV and paste it above: the header row becomes the keys and each further row becomes one object.
What happens to a comma inside a field?
It stays inside the value. A field holding a comma is wrapped in double quotes, so the reader takes it as part of the field rather than the end of it.
Can I convert a semicolon-separated or tab-separated file here?
Yes. Set the delimiter above to semicolon or tab and the field split follows it, and the converter records that the file was read by convention.
Do the numbers in a CSV file stay numbers in the JSON?
Under the text rule they become JSON strings, because a CSV file states no type. Switch the type rule to read them out, and the converter reports what that costs.
How do you convert CSV to JSON in Python?
Read the file with csv.DictReader, which takes the header row as the keys, then hand the rows to json.dumps. The line under CSV to JSON in Code does that.
How can I convert a CSV file to JSON in VS Code?
Through an extension, or by pasting the file here and copying the result back. The working above shows how each field reached its key.
Yes. The same file converts straight back in the JSON to CSV direction .
Related Conversions
The rest of the family sits on the data format converters.