Home / Code & Data / CSV to JSON Converter
04.13Code & Data

CSV to JSON Converter

Turn a spreadsheet export into clean JSON, or flatten JSON back into a CSV that Excel and Google Sheets open correctly. Quoted commas, line breaks inside cells and semicolon files are all handled, and nothing leaves your browser.

Updated
JSON output

How CSV to JSON conversion works

CSV is the format every spreadsheet, CRM, store platform and email tool can export. JSON is the format every API, JavaScript app and static site generator wants to read. Converting between them looks trivial, and for a three-row file it is. The trouble starts with real exports: a customer named Ruiz, Ana, a notes column with a line break in it, a European file that uses semicolons, or a ZIP code that loses its leading zero. This converter reads CSV the way a spreadsheet does, following RFC 4180, and writes JSON you can use without cleaning it up by hand.

By default the first row becomes the keys and every later row becomes one object, which is the shape most code expects. Switch to Arrays if you want each row as a plain list instead, for example to feed a chart library or a table component.

The CSV rules that break simple converters

Splitting each line on commas is the most common way to convert CSV, and it is wrong for any file that came out of a real system. A correct parser has to follow four rules:

  • Quoted fields can contain the delimiter. "Ruiz, Ana" is one value, not two.
  • A quote inside a quoted field is doubled. "Asked for the ""annual"" plan" means the value contains the word annual in quotes.
  • Quoted fields can contain line breaks. An address or a comment that spans two lines is still one cell, so you cannot split the file into lines first.
  • The delimiter is not always a comma. Excel in most of Europe and Latin America writes semicolons, because the comma is the decimal separator there. Database exports often use tabs or pipes.

Auto detect tests comma, semicolon, tab and pipe against the first rows and picks the one that produces the same number of columns on every row. If it guesses wrong, choose the delimiter yourself. The status line also tells you when rows have more or fewer values than the header, which usually points to a stray quote or an unescaped delimiter in the source file.

Numbers, leading zeros and other types

Everything in a CSV file is text. With type detection on, values that are clearly numbers become JSON numbers, true and false (in any letter case, so Excel's TRUE works) become booleans, and null becomes null. Three kinds of value are deliberately left as text because converting them destroys information:

  • Anything with a leading zero, such as the ZIP code 02134 or a phone number. When one value in a column has to stay text, the rest of that column stays text too, so a ZIP column never ends up half numbers and half strings.
  • Whole numbers too large for JavaScript to hold exactly (above 9,007,199,254,740,991), such as long order IDs.
  • Any value that was wrapped in quotes in the file. Quoting a value is how you tell the converter to keep it as text.

Turn detection off and every value stays a string, which is the safest choice when the JSON is going straight into another system that does its own validation.

Going the other way: JSON to CSV

JSON can nest and CSV cannot, so the converter flattens. A nested object becomes dotted column names: {"customer":{"name":"Ana"}} turns into a column called customer.name. An array inside a row is written as JSON text in a single cell so nothing is lost. The columns are the union of every key in every row, in the order they first appear, so rows that are missing a key get an empty cell instead of shifting the columns. If you paste an API response that wraps the list, such as {"data":[...],"total":40}, the list is found and used automatically.

Opening the result in Excel

Two things go wrong when a CSV meets Excel. First, accented characters turn into nonsense such as é because Excel assumes an old regional encoding unless the file starts with a byte order mark. The download option adds that mark, and Google Sheets, Numbers and LibreOffice simply ignore it. Second, a cell that starts with =, +, - or @ is treated as a formula, which is a known attack on anyone who exports user-submitted data (CSV injection). The Neutralize spreadsheet formulas option puts an apostrophe in front of those text values so they display as plain text. Numbers are not touched.

Doing the same thing in code

For a one-off file a converter is faster. For a job that runs every night, use a real CSV library instead of string splitting. Python has one built in:

import csv, json

with open('customers.csv', newline='', encoding='utf-8-sig') as f:
    rows = list(csv.DictReader(f))

with open('customers.json', 'w', encoding='utf-8') as f:
    json.dump(rows, f, indent=2, ensure_ascii=False)

The utf-8-sig encoding strips the byte order mark that Excel adds, which otherwise ends up glued to your first column name. Once you have JSON, the JSON Formatter will validate it and let you browse it as a tree, and the Text Diff Checker is a quick way to compare two exports.

Frequently asked questions

Is my CSV or JSON file uploaded to a server?

No. The file is read and converted by JavaScript in your browser, and it is never sent anywhere. You can load the page, disconnect from the internet and keep converting, which makes the tool safe for customer lists and other private exports.

Why did my ZIP codes and phone numbers stay as text?

On purpose. A value with a leading zero, such as 02134, would become 2134 as a number, so the converter keeps it, and the rest of that column, as text. Very large whole numbers and any value that was quoted in the file are kept as text for the same reason.

My CSV uses semicolons instead of commas. Will it convert?

Yes. Auto detect checks comma, semicolon, tab and pipe and picks the one that gives every row the same number of columns. Excel writes semicolons in regions where the comma is the decimal separator. You can also set the delimiter yourself.

How are nested JSON objects turned into CSV columns?

Nested objects are flattened into dotted column names, so customer.name and customer.email become two columns. Arrays inside a row are written as JSON text in one cell. Rows that are missing a key get an empty cell, so columns never shift.

Why does Excel show strange characters when I open the CSV?

Excel assumes an old regional encoding unless the file begins with a UTF-8 byte order mark, so é appears as é. Keep the Excel-friendly download option switched on and the mark is added for you. Other spreadsheet apps ignore it.