Skip to content
DoItJag

CSV vs JSON: the rules of each and how to convert safely

Published . Last reviewed .

In short:CSV is a flat table of text with a few quoting rules; JSON is a typed, nested structure with a strict grammar. CSV suits spreadsheets and bulk exports, JSON suits APIs and configuration. Most conversion problems come from types: leading zeros lost in a spreadsheet, nested objects that do not fit in a cell, and files saved in the wrong encoding.

Two formats, two shapes of data

CSV and JSON both carry data as plain text, so either can be opened in a text editor. The difference is the shape of what they can hold.

  • CSV holds a table: rows of fields, every field a piece of text. There are no types, no nesting and, strictly, no single standard.
  • JSON holds a tree: objects with named keys, arrays, strings, numbers, true, false and null, nested as deep as you like, with a strict grammar.

Converting between them is easy when the data is a simple table and gets harder as soon as it is not.

What the CSV rules actually say

The closest thing to a CSV specification is RFC 4180. It is an Informational RFC, which means it documents common practice rather than setting an Internet standard, and many programs differ from it. Its main rules:

  1. Each record is on its own line, ending in a line break (CRLF). The last line break is optional.
  2. An optional header line may come first, in the same format as the records.
  3. Fields are separated by commas, and every line should have the same number of fields. Spaces are part of the field.
  4. A field that contains a comma, a double quote or a line break must be enclosed in double quotes.
  5. Inside a quoted field, a double quote is written as two double quotes.

So the name Smith, Jane must be written as "Smith, Jane", and the text 5" screen becomes "5"" screen". A quoted field may also contain a line break, which is why splitting a CSV file on new lines is not a safe way to read it.

RFC 4180 also registers the media type text/csv, with an optional header parameter saying whether the first line is a header.

In practice you will meet files that ignore these rules. Spreadsheets in many European locales save with semicolons, because the comma is their decimal mark. Tab-separated files are common too. A good converter lets you choose or detect the delimiter.

What the JSON rules say

JSON is defined by RFC 8259, which is an Internet Standard (STD 90). Its grammar is small and strict:

  • A value must be an object, array, number or string, or one of the literals false, null and true.
  • Strings use double quotes. Single quotes are not JSON.
  • Numbers may not have leading zeros. 00412 is not a valid JSON number; it has to be the string "00412".
  • Trailing commas and comments are not allowed.
  • The names within an object “SHOULD be unique”. That is a recommendation, not a requirement, so a parser may keep the first or the last duplicate.
  • JSON exchanged between systems that are not part of a closed ecosystem “MUST be encoded using UTF-8”.

Because the grammar is strict, a single stray comma makes a JSON file invalid, while a CSV file with a stray comma just gains a column. A JSON formatter that reports the line and column of the error is the quickest way to find it.

When to use which

Need Better choice
Open in Excel or Sheets CSV
Send to or read from an API JSON
Nested records (an order with items) JSON
Large flat exports, one row per record CSV
Keep types (numbers, true/false, null) JSON
Configuration files JSON

CSV is smaller for flat data, because column names appear once in the header rather than in every record. JSON is self-describing: each value carries its key, and the structure is explicit.

Worked example: CSV to JSON

Take this semicolon-separated export, with a header row:

id;name;city;active;zip
1;"Smith, Jane";Pune;true;00412
2;Ravi;Delhi;false;

Converted with the header row as keys and type conversion on, the first record becomes:

{"id": 1, "name": "Smith, Jane", "city": "Pune", "active": true, "zip": "00412"}

Points to notice:

  • The quotes around Smith, Jane protected the comma; they are not part of the value.
  • 1 became a number and true a boolean, because type conversion was on.
  • 00412 stayed a string. Turning it into a number would lose the leading zero, and JSON does not allow 00412 as a number anyway.
  • Ravi’s empty zip became null. With type conversion off, it would be an empty string.

Our CSV to JSON converter works this way. Postal codes, phone numbers, account numbers and product codes should almost always stay as text.

Worked example: JSON to CSV

Going the other way, the problem is nesting. Take:

[{"id": 1, "name": "Asha", "address": {"city": "Pune", "pin": "411001"}, "tags": ["x", "y"]}]

A CSV cell can hold only text, so something has to give:

  • Nested objects can be flattened into dotted column names: address.city, address.pin.
  • Arrays can be written into one cell as JSON text, ["x","y"], which in CSV becomes "[""x"",""y""]" because of the quote-doubling rule.
  • Records with different keys need the union of all keys as the header, with empty cells where a record has no value.

The JSON to CSV converter flattens objects into dotted columns and writes arrays as JSON text in the cell.

Pitfalls that cost people time

Excel and leading zeros

If you open a CSV by double-clicking it, Excel guesses the type of each column. 00412 becomes 412, long numbers such as 16-digit card or account numbers lose their last digits, and some codes turn into dates. The file itself is fine; the damage happens when you save it again from Excel. Use the spreadsheet’s import feature and set those columns to Text.

Formula injection

A cell that starts with =, +, - or @ may be run as a formula when the CSV is opened in a spreadsheet. If the data comes from users, prefix such cells with an apostrophe or another safe character when you export. The JSON to CSV converter has an option for this.

Encoding

JSON should be UTF-8. CSV has no fixed encoding, and older spreadsheet programs may save in a regional code page, which turns names like “Müller” or Indian scripts into garbage when read as UTF-8. Some versions of Excel only recognise a UTF-8 CSV if it starts with a byte order mark. If accented letters or non-Latin scripts look wrong, the encoding is the first thing to check.

Numbers too big to keep

JSON numbers are often read into floating-point numbers, which hold whole numbers exactly only up to 9,007,199,254,740,991. Larger IDs should travel as strings in JSON as well as in CSV.

Line breaks inside fields

A comment or address with a line break is legal CSV inside quotes, but a script that splits on new lines will break the record in two. Use a real CSV parser.

A quick checklist before you convert

  1. Know your delimiter: comma, semicolon or tab.
  2. Decide which columns must stay as text: codes, IDs, postal codes, phone numbers.
  3. For JSON to CSV, decide how to handle nesting and arrays.
  4. Check the encoding is UTF-8.
  5. Validate the JSON result before you send it anywhere.

Both converters on this site run in your browser, so the data you paste or open is not uploaded.

Sources

Rules and rates change. Check the current notice or official page before you rely on a figure here. This guide is general information, not legal, tax or financial advice. Spotted a mistake? See our editorial and corrections policy.