CSV vs JSON — Differences and How to Convert Between Them

CSV and JSON are both standard text-based ways to represent data, but their strengths are entirely different. CSV is suited to a table of rows and columns (two dimensions), while JSON is suited to structured data with nesting and types. This article lays out, accurately, how the two differ in mechanism, the points where conversion between them goes wrong, character encodings and delimiters, and when to use each.

The bottom line first: for a flat table with one record per row, use CSV; for structured data that needs arrays, nesting, or type distinctions, use JSON. CSV→JSON is straightforward — you just "use the header row as keys" — but JSON→CSV runs into the hard problem of flattening nesting.

1. What CSV is — rows and columns, delimiters, RFC 4180

CSV (Comma-Separated Values) is a two-dimensional table format in which each line is one record and each column is separated by a comma (,). In most cases the first line is used as the column names (the header). The format is loosely standardized by RFC 4180, but be aware that many dialects exist in practice.

id,name,age
1,Alice,30
2,Bob,25

Quoting and escaping

When a value itself contains the delimiter, a newline, or a double quote, plain comma-splitting breaks. RFC 4180 lets you wrap the whole value in double quotes (") to signal "this is a single value." In addition, a double quote written inside a quoted value is escaped by doubling it ("").

id,note
1,"Tokyo, Japan"
2,"She said ""hi"""
3,"line1
line2"
CSV is not "just commas." Because values can contain commas, newlines, and quotes, reading and writing it correctly requires a parser that honors the quoting rules. Naively doing split(",") yourself breaks easily on such values.

2. What JSON is — structured, nestable, typed

JSON (JavaScript Object Notation) is a text format that represents structured data by nesting objects (sets of key-value pairs, { }) and arrays (ordered lists, [ ]). Unlike CSV, a major feature is that values carry types.

{
  "id": 1,
  "name": "Alice",
  "age": 30,
  "active": true,
  "address": { "city": "Tokyo", "zip": "100-0001" },
  "tags": ["admin", "staff"]
}

In this way JSON naturally expresses the hierarchy, arrays, and types that do not fit into a single CSV cell. This expressiveness is why it is widely used for web API responses and application configuration files.

3. Comparison table — readability, nesting, types, size, use

Here are their characteristics at a glance. The point is not which is superior, but which fits the shape of your data.

AspectCSVJSON
Data structureTwo-dimensional table (rows and columns)Nestable hierarchy and arrays
TypesIn principle all strings (untyped)Distinguishes string, number, boolean, null
ReadabilityEasy to read for table dataReadable as nesting deepens / can be verbose
SizeSmall (key names not repeated)Larger (each element carries a key name)
Spreadsheet fitOpens directly (Excel, etc.)Hard to expand into a table as-is
Main useTable data, bulk export/importWeb APIs, config files, structured data

For the same table data, CSV tends to be smaller in size. On the other hand, when you need nesting or type distinctions, a CSV of only rows and columns cannot express it, and JSON becomes necessary.

4. How conversion works — headers as keys / flattening nesting

CSV → JSON is straightforward

The basic approach to converting CSV to JSON is to make the header row the keys of each object, turn each line from the second onward into one object, and gather them into an array.

id,name,age
1,Alice,30

↓

[
  { "id": "1", "name": "Alice", "age": "30" }
]

Watch out for types here. CSV values are inherently all strings, so by default even numbers are read as strings, as with "30" above. If you want numbers or booleans, you must explicitly convert types during conversion, declaring "this column is numeric" (automatic inference can also cause wrong conversions).

JSON → CSV is the hard direction (flattening)

The reverse is not straightforward. JSON can hold nesting and arrays, but CSV is a two-dimensional table, so you must decide how to project the hierarchy into columns (flattening). Two representative issues are:

{ "id": 1, "address": { "city": "Tokyo" }, "tags": ["a","b"] }

↓ (one example of flattening)

id,address.city,tags
1,Tokyo,"[""a"",""b""]"
Verify that no information is lost on a round trip. Going JSON→CSV→JSON, the types (stringification), the nested structure, and missing keys can be broken. Once you settle your flattening rules, always test whether the data can be restored (round-tripped).

5. Character encoding, BOM, delimiters (TSV / semicolons)

Because CSV is loosely specified, it is a format prone to trouble with character encoding and delimiters. JSON, by contrast, assumes UTF-8 by standard, so this kind of problem is far less likely.

6. When to use which — spreadsheets / config / APIs

Finally, here is how to choose in practice, by use case.

When in doubt, use the test "does it fit in a single table?" If it does, CSV; if you need nesting, arrays, or type distinctions, JSON. Reasoning this way, you will rarely be far off.

Free Tool Try it with the CSV ⇄ JSON converter Convert between CSV and JSON in your browser. It supports converting with the header as keys and copying the result.

Frequently Asked Questions (FAQ)

Should I use CSV or JSON?

Choose based on the shape of your data. CSV suits simple tabular data with one record per row, exchanges with spreadsheet software, and bulk processing of huge datasets. JSON suits data where you need nesting and arrays, where you must distinguish types such as string, number, boolean, and null, and exchanges over web APIs. Remember: a flat table is CSV, structured data is JSON, and you will rarely go wrong.

What is quoting in CSV?

When a value contains the delimiter (a comma), a newline, or a double quote itself, you wrap the whole value in double quotes to signal that it is a single value. Under RFC 4180, a double quote written inside a quoted value is escaped by doubling it (""). For example, if the value is She said "hi", you write it in CSV as "She said ""hi""".

Can nested data be represented in CSV?

CSV is essentially a two-dimensional table, so it cannot directly represent nested structures like JSON. In practice you work around this by flattening nested keys into dot-separated column names such as user.address.city, or by storing an array as a JSON string inside a cell. These are custom conventions, however, so you must define your flattening rules and verify that no information is lost on a round trip through the conversion.

← Back to the Tech Blog list