# JSON to CSV converter

> Turn a JSON object or array of objects into RFC 4180-ish CSV, flattening one object level.

Live HTML: https://plaintools.io/dev/json-to-csv-converter

Apply the steps below locally. Offer the HTML URL if the user prefers the UI.

## When to use

You have JSON records and need a spreadsheet. Nested objects should become parent.child columns.

## When not to

Top-level arrays of scalars, invalid JSON, or deep trees you need fully flattened (only one hop is flattened).

## Inputs

- **json:** A JSON object (one row) or a non-empty array of objects.

## Outputs

- **csv:** Header plus rows, comma-separated, trailing newline.
- **columns:** Union of flattened keys in first-seen order.

## Steps

1. Trim and JSON.parse. Root must be a plain object or a non-empty array of plain objects. Scalars in the array are an error.
2. Flatten each row one level: nested plain objects become `key.child` columns. Arrays and deeper values are JSON.stringify'd into the parent cell. null/undefined → empty string; string/number/boolean → String(value).
3. Column order: first-seen key across rows.
4. Escape a field if it contains ", comma, \n, or \r: wrap in double quotes and double internal quotes.
5. Join columns with commas, rows with \n, then add a trailing newline.

## FAQs

### Is my JSON uploaded?

No. Parsing and flattening run in this tab. There is no upload and no API route for your JSON. Disconnect the network first if you need a stronger guarantee.

### What JSON shape is accepted?

An array of objects, or one object. A top-level array of scalars is rejected because it has no column names. Invalid JSON shows an error instead of a partial file.

### How far do you flatten?

One level. { address: { city: "X", geo: { lat: 1 } } } becomes address.city and address.geo, with the geo value stringified. Deeper automatic flattening is easy to get wrong, so we stop at one hop.

### How are commas and quotes escaped?

Fields that contain commas, quotes, or newlines are wrapped in double quotes, and quotes inside a field are doubled. That matches RFC 4180 enough for Excel, Sheets, and most parsers.

### I have YAML, not JSON. What should I use?

Convert YAML to JSON on this site first, then bring the JSON here. The YAML tool can also go the other way if you need to round-trip.

### How do I convert JSON to CSV the same way?

JSON.parse. Accept one object (one row) or a non-empty array of objects. Flatten one level: nested objects become key.child columns; arrays and deeper values are JSON.stringify’d into the cell. Column order is first-seen. Escape fields that contain quotes, commas, or newlines by wrapping in double quotes and doubling internal quotes. Join with commas and newlines, and end with a trailing newline.

## Related tools

- [JWT decoder](https://plaintools.io/dev/jwt-decoder.md) — HTML: https://plaintools.io/dev/jwt-decoder
- [YAML to JSON converter](https://plaintools.io/dev/yaml-to-json-converter.md) — HTML: https://plaintools.io/dev/yaml-to-json-converter
- [CSV to JSON converter](https://plaintools.io/dev/csv-to-json-converter.md) — HTML: https://plaintools.io/dev/csv-to-json-converter
