Developer Tools

CSV to JSON: how to convert with commas, quotes and line breaks inside fields, and the mistakes that corrupt data

Turn a spreadsheet export into JSON without losing leading zeros, splitting “Pune, Maharashtra” in two, or breaking on a stray quote. Covers the three JSON shapes, delimiters, encodings, why big IDs must stay as text, and how much bigger JSON gets.

By the Toolcloak team22 October 20264 min read
In this article
  1. What a conversion does, in four steps
  2. The same data in three JSON shapes
  3. Four things that break a conversion
  4. Empty cells, dates and booleans
  5. Going the other way, and checking your result

A CSV file looks like the simplest format in the world: values separated by commas, one row per line. Converting it to JSON should be easy too. It is, until a cell contains a comma, a quote, a line break or a leading zero. Those four are behind almost every broken conversion. This guide shows what a correct conversion does and how to spot a bad one.

What a conversion does, in four steps

From CSV rows to a JSON array
  1. 1Read the header rowid,name,city become the keys.
  2. 2Parse each line properlyRespect quotes, so a comma inside quotes is not a separator.
  3. 3Build one object per row{ "id": "1", "name": "Asha", "city": "Pune" }.
  4. 4Wrap them in an array[ {…}, {…}, {…} ], ready for code, an API or a database.

Three rows of data give three objects. The header row is not a record, it is the list of keys.

Paste CSV, choose the delimiter and indent, and get clean JSON. Handles quoted values and works in your browser.Open CSV to JSON

The same data in three JSON shapes

ShapeLooks likeBest for
Array of objects[{"id":"1","name":"Asha"}, …]APIs, most code. Each row is self-describing.
Array of arrays[["id","name"],["1","Asha"], …]Compact data, charts, when the column order is fixed.
Object keyed by an ID{"1":{"name":"Asha"}, …}Looking up a row by its ID quickly.

Most tools output the first shape. Choose the one your code expects, because changing it afterward means rewriting the loop.

Characters used for the same 3-row table
52CSV86JSON arrays122JSON objects195Objects, pretty-printed

Repeating the keys in every object costs space. On big files this is why JSON is often compressed or sent as arrays. Pretty-printing adds spaces and indents for people to read.

Four things that break a conversion

1. A comma inside a field

The row 3,Meera,"Jaipur, Rajasthan" has three fields, not four. The quotes say the comma belongs to the text.

Splitting `3,Meera,"Jaipur, Rajasthan"`
Naive split on commas4 pieces
  • 3
  • Meera
  • "Jaipur
  • Rajasthan"
Real CSV parser3 fields
  • 3
  • Meera
  • Jaipur, Rajasthan

If your JSON has extra columns on some rows, a comma inside a field is the first thing to check.

2. A quote or a line break inside a field

A double quote inside a quoted field is written as two: "He said ""hello""" means He said "hello". A line break inside quotes is part of the text, not the end of the row. Addresses and comments often have them.

3. Numbers that are really text

A pincode like 0110xx, a phone number like 098765… or an employee ID like 007 loses its leading zeros if it is turned into a number. A long ID can also be rounded: JSON numbers in most code are stored as doubles, which are exact only up to 9,007,199,254,740,992. A 19-digit number such as 1234567890123456789 comes back as 1234567890123456800. If it is an identifier and you will not do sums on it, keep it as text.

4. The wrong delimiter or encoding

In many countries Excel writes CSV with semicolons because the comma is the decimal mark. If everything lands in one column, change the delimiter. If Hindi or accented letters turn into odd symbols, save the file as UTF-8. Some Excel files start with an invisible byte-order mark that can make the first key look wrong, such as \ufeffid.

Empty cells, dates and booleans

  • Empty cells usually become an empty string. Decide whether your code wants "" or null and fix it after.
  • Dates stay as text. 01/02/2026 could be 1 February or 2 January, so agree a format such as 2026-02-01 before you convert.
  • TRUE and FALSE from spreadsheets arrive as text. Convert them to real booleans in code if needed.
  • Duplicate column names overwrite each other because a JSON object can hold a key only once. Rename them first.

Going the other way, and checking your result

The JSON to CSV tool turns an array of objects back into a spreadsheet. Check the JSON you get with why your JSON won’t parse, which explains the commonest syntax errors, and use the JSON formatter to read it more easily.

Related guides: how to convert JSON to CSV.

Questions people ask

How do I convert CSV to JSON?

Paste the CSV into a converter that uses the first row as keys. Each following row becomes an object, and all the objects are placed in an array. Check that the number of objects matches the number of data rows.

How do I handle commas inside CSV fields?

Wrap the field in double quotes, as in "Pune, Maharashtra". A proper CSV parser treats the quoted comma as part of the text. Splitting a line on every comma will break these rows.

Why is my CSV all in one column?

The file probably uses semicolons or tabs, not commas. Change the delimiter setting to match the file.

How do I keep leading zeros when converting CSV to JSON?

Keep the field as text. Do not convert it to a number. Numbers such as 007 or a pincode with a leading zero lose the zero when parsed as numeric.

Is JSON bigger than CSV?

Usually yes, because each object repeats the column names. A 3-row example is 52 characters as CSV and 122 as compact JSON objects, about 2.3 times.

Can I convert Excel to JSON?

Save the sheet as CSV (UTF-8), then convert the CSV. Check that dates and long numbers look right before you export.

What JSON format should I use?

An array of objects is the standard for APIs and most code. Use an array of arrays for compact data or an object keyed by ID for fast lookups.