Back to blog
Conversion8 min read

How to Convert CSV to JSON: A Practical Guide

Convert CSV to JSON correctly: header-to-key mapping, handling delimiters and quoted fields, type inference, nested output, and the edge cases (commas, newlines, empty values) that break naive conversion.

By Saifur Rahman Shochib

Spreadsheets and exports speak CSV; APIs and apps speak JSON. Converting between them is one of the most common data tasks there is — and one of the easiest to get subtly wrong, because CSV's quoting and delimiter rules are messier than they look. This guide explains how CSV becomes JSON, the choices you make along the way, and the edge cases that trip up naive converters.

How CSV maps to JSON

The standard conversion treats the first CSV row as the header. Each subsequent row becomes a JSON object whose keys are the header names and whose values are that row's cells. The whole file becomes an array of these objects — exactly the array-of-objects shape that APIs and code expect. So three data rows produce a three-element JSON array.

CSV input (first row is the header)csv
id,name,city
1,Ada,Paris
2,Linus,Helsinki
Becomes an array of objectsjson
[
  { "id": "1", "name": "Ada",   "city": "Paris" },
  { "id": "2", "name": "Linus", "city": "Helsinki" }
]

Delimiters and quoted fields

CSV is not always comma-separated — semicolons and tabs are common, especially in European locales where commas are decimal separators. A good converter lets you choose the delimiter. The harder problem is quoting: a field containing a comma or newline is wrapped in double quotes, and embedded quotes are doubled. A naive split on commas mangles these fields, so proper CSV parsing must respect quotes rather than just splitting text.

Quoted fields keep commas and doubled quotes intactcsv
id,note
1,"Hello, world"
2,"She said ""hi"""

Type inference

CSV has no types — every cell is text. JSON does, so conversion has to decide whether the string 42 becomes the number 42 or stays "42". Type inference is convenient but risky: a ZIP code like 01234 loses its leading zero if turned into a number, and a value like 1.10 may drop the trailing zero. Choose inference when you want typed numbers and booleans, and keep values as strings when identifiers and codes must be preserved exactly.

Handling messy CSV

  • Embedded commas and newlines inside quoted fields must stay within one value, not split the row.
  • Empty cells can become an empty string, null, or a missing key — decide which your consumer expects.
  • Inconsistent column counts across rows signal a malformed file that needs cleaning first.
  • A byte-order mark or non-UTF-8 encoding can corrupt the first header key.
  • Duplicate header names collide, since JSON object keys must be unique.

Flat output versus nested

Basic conversion produces flat objects with one key per column. If your CSV columns use a convention like address.city or address_city, some workflows expand those into nested JSON objects. When you need the reverse — taking nested JSON down to flat columns for a spreadsheet — flattening handles it. Decide up front whether your target system wants flat records or nested structures.

Converting in practice

For a quick, reliable conversion, paste your data into a CSV to JSON tool that parses quotes and delimiters correctly and lets you control type inference — far safer than a spreadsheet copy-paste. The JSON to CSV tool handles the reverse when you need to export JSON back to a spreadsheet, and a formatter or validator confirms the result is well-formed before you use it.

Frequently asked questions

How do I convert CSV to JSON?

Treat the first row as headers; each following row becomes a JSON object keyed by those headers, and the file becomes an array of objects. A CSV-to-JSON converter does this while correctly handling quoted fields and your chosen delimiter.

What happens to quoted fields with commas in CSV?

A correct parser keeps a quoted field as a single value even if it contains commas or newlines, and unescapes doubled quotes. Splitting on commas without respecting quotes corrupts these fields, which is why proper CSV parsing matters.

Should CSV numbers become JSON numbers?

It depends. Type inference turns 42 into a number, which is convenient, but it can damage identifiers — a ZIP code like 01234 loses its leading zero. Keep codes and IDs as strings, and infer types only for genuine numeric data.

How are empty CSV cells represented in JSON?

Converters vary: an empty cell can become an empty string, null, or an omitted key. Choose the behavior your consumer expects, and be consistent across the file.

Can I convert CSV with semicolons or tabs to JSON?

Yes. Choose the correct delimiter in your converter. Semicolon-separated files are common in locales that use the comma as a decimal separator, and tab-separated values (TSV) work the same way.

Related tools

Try these tools

Keep reading

Related guides