ToolSite
All posts

How to Convert JSON to CSV and Back Without Data Loss

Convert JSON to CSV and back while preserving nested data. Handle arrays, nested objects, and type edge cases with our free converter for accurate round trips.

By ToolSite4 min readguides

JSON and CSV: Different Animals

JSON is hierarchical. Arrays can contain objects, objects can contain more arrays, and nesting can go arbitrarily deep. CSV is flat: a two-dimensional grid of rows and columns with no concept of nesting at all.

Converting between the two means making decisions about how to flatten nested data or how to reconstruct it. This is not a lossless round-trip by default. Every conversion requires choices, and the wrong choice can lose data silently.

JSON to CSV: Flattening

Start with an array of objects, the natural JSON shape for tabular data:

[
  {"name": "Alice", "email": "[email protected]", "role": "admin"},
  {"name": "Bob", "email": "[email protected]", "role": "user"}
]

This converts cleanly to:

name,email,role
Alice,[email protected],admin
Bob,[email protected],user

No data loss. Every key becomes a column header. Every value becomes a cell. This is the happy path. It works perfectly when your JSON is already flat and tabular.

Nested Objects: The Flattening Decision

Now add a nested address:

[
  {
    "name": "Alice",
    "email": "[email protected]",
    "address": {"city": "Paris", "country": "France"}
  }
]

A naive converter produces something like [object Object] in the address cell. That is useless. The right approach is to flatten the nested object into separate columns:

name,email,address.city,address.country
Alice,[email protected],Paris,France

Some converters use dot notation for nested object keys. Others use underscores or double underscores. The goal is the same: each leaf value gets its own column. When you convert back to JSON, the converter reverses the flattening and reconstructs the nested structure.

Deeply nested structures create wide CSV files with many columns. A JSON object nested four levels deep might produce a column header like user.profile.address.geo.lat. Most spreadsheet applications handle this fine, but it can be awkward to scan visually.

Arrays Inside JSON Objects

What about arrays?

[
  {"name": "Alice", "tags": ["engineer", "python", "rust"]}
]

A CSV has no array type. You have three common strategies:

  • Join with a delimiter: store engineer;python;rust in a single cell. Semicolons or pipes are typical choices when commas might appear in values. This keeps the row count unchanged and works well for display.
  • Expand into multiple columns: produce tags_0, tags_1, tags_2. This only works if the array length is consistent across all rows. If one record has 5 tags and another has 2, you get sparse columns.
  • Duplicate rows: one row per tag. A record with 3 tags becomes 3 rows. This explodes the row count but works well for database imports and pivot tables.

Choose based on what the CSV consumer expects. Spreadsheet users prefer delimited cells. Database imports often prefer duplicated rows. API consumers may want the delimiter approach so they can split on the other end.

CSV to JSON: The Reverse Trip

Converting CSV back to JSON involves the same decisions in reverse. When you see columns named address.city and address.country, the converter needs to know they should be re-nested into an address object.

A flat CSV like:

name,age
Alice,30
Bob,25

Becomes:

[
  {"name": "Alice", "age": "30"},
  {"name": "Bob", "age": "25"}
]

Note that age is a string. CSV has no type system: everything is text. If you need integers, parse them after conversion or use a tool that supports type inference. Some converters guess types by checking whether a column contains only digits, but this is heuristic and can misfire on zip codes or ID numbers.

Edge Cases to Watch For

  • Commas in values: values like "Smith, Jr." break naive CSV parsing. Always wrap values containing commas or newlines in double quotes. RFC 4180 specifies this, and most CSV libraries handle it correctly, but hand-rolled parsers often miss it.
  • Quotes in values: double quotes inside a quoted field must be escaped by doubling them. "She said ""hello""" represents She said "hello". If you see stray quotes in your imported data, check whether the exporter escaped them properly.
  • Header mapping: if your JSON keys don't match the column names you want, you need a mapping step between the two formats. Some converters let you supply a header override.
  • Type information: JSON has null, true, false, and numbers. CSV flattens all of these to strings. A round-trip may lose type fidelity. If you need to preserve types, store a separate schema file or use a format that retains type metadata.
  • Empty values: JSON null vs an empty string "" can look identical in CSV. Decide on a convention (empty cell for both, or a sentinel like NULL) before converting large datasets.

Try it yourself: open the CSV to JSON Converter. Paste a CSV snippet from a spreadsheet and convert it to JSON. Then paste the JSON output back into the JSON-to-CSV direction and compare the result. Notice how nested structures require flattening decisions and how type information can shift during the round trip.

Related Reading