ToolStation

How to Convert Nested JSON to CSV Without Losing Important Data

Choose a flattening, JSON-cell, or row-expansion strategy for nested JSON, and understand where CSV conversion becomes lossy.

Nested JSON cannot be mapped to CSV in one universally correct way. JSON can contain objects inside objects, arrays inside objects, and arrays of objects with different shapes. CSV is a flat table. Before converting, choose what one row means and how child values should be represented. Flattening nested objects into dotted columns is convenient for analysis, keeping a nested value as JSON text preserves its structure, and expanding arrays into more rows can multiply record counts. Each choice is useful for a different task.

Start with the row you want

Suppose an order has one customer and several line items:

{
  "orderId": "O-19",
  "customer": {"name": "Asha", "region": "South"},
  "items": [
    {"sku": "B-1", "quantity": 2},
    {"sku": "P-3", "quantity": 1}
  ]
}

There are at least two reasonable row concepts. One row per order keeps the customer and order metadata together, but then the items array needs to remain encoded in a single cell or be split across numbered columns. One row per item makes product analysis easier, but repeats order and customer fields and turns an order with three items into three rows.

Pick a row definition based on the question the spreadsheet should answer. If analysts need to calculate item quantities, one row per item may be practical. If they need to count orders, preserve an order identifier on every expanded line and remember to count distinct IDs, not rows.

Flatten nested objects into columns

A nested object often maps cleanly to a path-like header. The customer.name and customer.region values can become two columns. The Nested JSON to CSV tool uses dotted paths for nested object properties. This keeps each record to one row when the nested values are scalar fields.

Flattening has edge cases. A literal property named customer.name could collide with a path produced by a nested customer object containing name. Arrays do not flatten the same way as objects because indices can vary between records. A first item could become items[0].sku, but a record with no items would have no such value and a long array could require many columns.

A stable schema helps. Define which paths become columns and how absent values are filled. A generated union of every observed path can work for exploratory data, but a new key can change the column set on the next export. For repeatable ingestion, specify the intended columns and order.

Keep nested arrays as JSON text

If a nested array should stay together, serialize it as JSON text inside one CSV cell. For example, the items column could contain:

[{"sku":"B-1","quantity":2},{"sku":"P-3","quantity":1}]

CSV quoting then protects commas and quotes inside that JSON string. The CSV reader sees one cell; a later program can parse its contents as JSON. This approach preserves more structure than joining values with a comma, which becomes ambiguous when elements themselves contain commas.

The trade-off is that spreadsheet users cannot easily filter item properties without another parsing step. A JSON cell is convenient for archival or transfer when structure matters, while expanded columns are more useful for simple analysis. Document the column format so the next consumer knows that a cell contains a second serialized language.

Expand arrays into rows only with a rule

Row expansion is useful when an array represents repeated child records. A safe export repeats the parent key for each child and defines what happens when the array is empty. For the order above, an expanded result could include orderId, customer.name, item.sku, and item.quantity. Both item rows would retain O-19 so downstream code can group them.

Multiple arrays create a harder problem. If an object has three items and two tags, independently expanding both arrays can produce six rows through a Cartesian product. That may look like duplicated data, but it is the mathematical result of combining each item with each tag. If the arrays are related by position or ID, use that relationship explicitly rather than blindly expanding them.

An empty array can mean no child records. The exporter must decide whether that produces zero rows, one parent-only row, or a sentinel row. Each convention affects counts. For analysis, keeping a separate parent table and child table may be more accurate than forcing a relational structure into one CSV.

Missing, null, and scalar types

Nested conversion must preserve or document the distinctions among absent paths, explicit nulls, empty strings, zeros, and false. Many CSV exports use an empty cell for both missing and null. That can be adequate for reports but destructive for updates where null means “clear this field.” Use a documented null token or presence indicator if the difference matters.

JSON has strings, numbers, booleans, and null; CSV generally presents each field as text. The receiving tool may infer a type and change it. Spreadsheet programs can remove leading zeros, reinterpret date-shaped strings, or display long numbers in scientific notation. Quote marks in CSV protect field boundaries, not numeric formatting semantics. Treat identifiers as text when leading zeros and exact digits matter.

Flattening can lose meaning

Even a syntactically correct CSV can lose structure. Dotted paths may conflict with literal keys. Null and missing may merge. Array order may be hidden in joined text. Duplicate array values may disappear if someone converts them to a set. A number such as 2.00 may be emitted as 2. The conversion is only reversible if the encoding rules preserve these distinctions and the consumer knows those rules.

Keep the original JSON when the export is for analysis rather than replacing the source of truth. If a lossless round trip is required, CSV may be a poor target for deeply nested content. A database with child tables, JSON Lines, or another structured format may fit better.

Handle large nested exports carefully

Flattening every record can create thousands of columns if keys are irregular. Expanding arrays can create far more rows than input objects. Estimate the output dimensions before opening a spreadsheet, and inspect the maximum array length and unique path count. Browser tools also need memory for the input, parsed objects, and output string.

For a repeated or large conversion, use a local script with explicit schema rules and incremental writing. A streaming implementation can process one record at a time rather than holding the whole output in memory. Test it on objects with missing fields, empty arrays, multiple nested objects, and unusually long values. The browser JSON Tree Viewer can help inspect a representative sample before choosing a mapping.

A decision checklist

  1. Define what each output row represents.
  2. Flatten stable scalar object paths into columns.
  3. Keep arrays as JSON text unless row expansion has a clear analytical purpose.
  4. If expanding, preserve parent IDs and define empty-array behavior.
  5. Document how null, missing, and empty values differ.
  6. Test keys containing dots, commas, quotes, and Unicode.
  7. Keep a JSON source copy if the CSV is not lossless.

Use the JSON to CSV page for flat records and the nested converter when a flattened table is appropriate. For language-based pipelines, see the Python, JavaScript, and PowerShell examples.

Conclusion

Nested conversion is a data-modeling decision, not just a delimiter operation. Flatten objects for stable scalar analysis, preserve arrays as JSON text when structure matters, and expand arrays only when the new row meaning is clear. Document any lossy choices and retain the source JSON whenever you may need information that a flat table cannot express.