# Convert CSV to JSON without losing field values

Canonical page: https://datalens.flazetech.in/guides/csv-to-json-preserve-values

By [DataLens by Flazetech](https://datalens.flazetech.in/about).

To convert CSV to JSON, use a header row with a unique name for each column and keep the same number of fields in every record. DataLens maps headers to object keys and preserves CSV values as strings, including leading zeroes. Quoted commas and quoted line breaks stay inside their fields.

[Open CSV to JSON converter](https://datalens.flazetech.in/csv)

## How CSV headers become JSON keys

Choose CSV → JSON, paste the source and select Convert to JSON. The first row supplies the property names, and every later record becomes an object in an array. Inspect the output, then copy it or download the .json file.

The converter keeps field values as strings instead of guessing their types. This preserves identifiers such as 0012. A value spelled false remains the string "false", not a JSON boolean. Convert selected fields deliberately in your application if it requires numbers or booleans.

### CSV input

```text
id,name,city
0012,Alex,"New York, NY"
0013,Sam,London
```

### JSON output

```text
[
  {"id":"0012","name":"Alex","city":"New York, NY"},
  {"id":"0013","name":"Sam","city":"London"}
]
```

## Why splitting on every comma breaks CSV

CSV fields can contain the delimiter itself. A double-quoted field can also contain a line break, and an embedded double quote is represented by two double quotes. A parser must track quoted fields; a simple split on commas or newlines can turn one record into several broken pieces.

### An escaped quote inside a field

```text
name,note
Alex,"Said ""hello"""
```

### The resulting JSON value

```text
{"name":"Alex","note":"Said \"hello\""}
```

Reference: [RFC 4180: common CSV quoting conventions](https://www.rfc-editor.org/rfc/rfc4180).

## Fix duplicate headers and inconsistent rows

DataLens rejects duplicate header names because two fields cannot safely map to the same object property. Rename repeated columns before converting. Blank headers receive generated column names, which still need to be unique in the final header set.

A row with too many or too few fields is rejected instead of silently dropping data. Check an unmatched quote, an unquoted delimiter in a value, or an extra delimiter at the end of the row. The delimiter detector supports commas, tabs, pipes and semicolons; inspect a small sample if the source format is unusual.

| Symptom | Check |
| --- | --- |
| Duplicate header error | Give every column a different name. |
| Unexpected field count | Match each record to the header width and check quoting. |
| An ID is missing leading zeros | Check whether the source spreadsheet changed it before export. |
| A boolean appears in quotes | CSV values intentionally remain strings. |

## Convert a JSON array back to CSV

Switch to JSON → CSV and provide an array of objects. The converter uses the union of their property names as headers. Values that contain commas, quotes or line breaks are escaped for CSV. Nested objects and arrays become JSON text inside a cell rather than additional columns.

Review the result in the program that will consume it. If you need one column joined into a single line, use the comma separator instead. If you need to inspect nested structures, keep the JSON and use the formatter or JSONPath tester. Each choice preserves a different part of the original structure.

## Related tools

- [JSON formatter & validator](https://datalens.flazetech.in/json)
- [Delimiter converter](https://datalens.flazetech.in/delimiter)
- [Comma separator](https://datalens.flazetech.in/convert-column-to-comma-separated-list)
