# 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.

Published 2026-10-22 by Toolcloak. 4 minute read. Original: https://toolcloak.com/articles/csv-to-json-how-to-convert-with-commas-quotes-and-line-breaks-inside-fields-and-the-mistakes-that-corrupt-data
Related free tool: [CSV to JSON](https://toolcloak.com/tools/csv-to-json)

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.

> **In short**
> - **The usual output is an array of objects:** each row becomes `{ "column": "value" }` using the header row as keys.
> - **Fields with commas or line breaks must be wrapped in double quotes,** and a quote inside is written twice. A real CSV parser handles this. Splitting on commas does not.
> - **Keep IDs, phone numbers and pincodes as text.** Converting them to numbers drops leading zeros and can round long digits.
> - **JSON is bigger than CSV** because every row repeats the column names: about 2.3 times here.

## What a conversion does, in four steps

**From CSV rows to a JSON array**

1. **Read the header row.** `id,name,city` become the keys.
2. **Parse each line properly.** Respect quotes, so a comma inside quotes is not a separator.
3. **Build one object per row.** `{ "id": "1", "name": "Asha", "city": "Pune" }`.
4. **Wrap 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.*

Try it: [CSV to JSON](https://toolcloak.com/tools/csv-to-json). Paste CSV, choose the delimiter and indent, and get clean JSON. Handles quoted values and works in your browser.

## The same data in three JSON shapes

| Shape | Looks like | Best 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**

|  | Value |
| --- | --- |
| CSV | 52 |
| JSON arrays | 86 |
| JSON objects | 122 |
| Objects, pretty-printed | 195 |

*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 commas | Real CSV parser |
| --- | --- |
| 4 pieces | 3 fields |
| 3 | 3 |
| Meera | Meera |
| "Jaipur | Jaipur, Rajasthan |
| 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`.

> **A quick test:** After converting, check the number of objects against the number of data rows, and open the last record. If the last record is missing or half-filled, a quote earlier in the file was left open.

## 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](https://toolcloak.com/tools/json-to-csv) turns an array of objects back into a spreadsheet. Check the JSON you get with [why your JSON won’t parse](https://toolcloak.com/articles/why-your-json-wont-parse-and-how-to-find-the-bad-comma), which explains the commonest syntax errors, and use the [JSON formatter](https://toolcloak.com/tools/json-formatter) to read it more easily.

> **Private data stays private:** The converter runs in your browser, so a customer list or a salary sheet is not sent to a server. For anything confidential, that is the safer way to convert.

Related guides: [how to convert JSON to CSV](https://toolcloak.com/articles/how-to-convert-json-to-csv-nested-objects-lists-inside-cells-and-opening-the-file-in-excel-without-broken-columns).

## Frequently asked questions

### 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.

