Data Conversion6 min read

How to Convert JSON to CSV (and CSV Back to JSON)

JSON and CSV are both common ways to exchange data, but they organize information differently. JSON represents objects and arrays; CSV represents rows and columns. Choosing the right structure before converting helps prevent missing fields and unexpected spreadsheet values.

When JSON and CSV are useful

JSON is commonly used by APIs and applications because it can represent nested objects, arrays, strings, numbers, booleans, and null values. CSV is a simpler tabular format that works well when each record can be represented as a row with named columns.

A conversion is most straightforward when the JSON input is an array of objects and each object represents one record. For example, a list of customers with a name, email address, and status can be represented as a table.

Example JSON input

[
  { "name": "Ada", "role": "Engineer", "active": true },
  { "name": "Lin", "role": "Designer", "active": false }
]

Corresponding CSV output

name,role,active
Ada,Engineer,true
Lin,Designer,false

How JSON arrays become CSV rows

Each object in the array becomes one CSV record. The converter collects the object property names to create a header row. If records contain different properties, the header row includes the distinct keys found across the objects, and a record without a particular key receives an empty cell for that column.

The order of the columns follows the order in which property names are first encountered while scanning the input records. Review the output before importing it into another system, especially when downstream workflows expect a fixed column order.

  • Use an array at the top level, not a single JSON object.
  • Each array item must be an object; nested arrays cannot be used as separate top-level records.
  • Simple values such as strings, numbers, and booleans are written as cell text.
  • Nested objects and arrays are serialized as JSON text inside a CSV cell.
  • Null values become empty cells because CSV has no native null type.

Why CSV quoting matters

A comma normally separates CSV columns, so a field containing a comma must be quoted. Double quotes inside a quoted field are escaped by doubling them. A quoted field may also contain a line break without starting a new record.

These rules are important when names, descriptions, addresses, or notes contain punctuation. A correctly quoted CSV field keeps its contents together in one cell.

CSV fields containing commas and quotes

name,notes
"Lee, Jordan","Said ""hello"""

Convert CSV back to JSON

For CSV-to-JSON conversion, the first row is treated as the header row. Every header must be present and unique. The converter then creates one JSON object for each remaining data row, using the header names as object keys.

CSV does not store JavaScript or JSON data types. As a result, values in the generated JSON are strings, including text that looks like a number or boolean. An empty CSV cell becomes an empty string rather than a JSON null value.

  • Include a header row followed by at least one data row.
  • Give every column a non-empty, unique header.
  • Check that each data row has no more values than the header row.
  • If a row contains fewer values, the missing trailing fields are filled with empty strings.
  • Keep values that contain commas, quotes, or line breaks inside properly quoted CSV fields.

CSV input

name,age,active
Ada,36,true
Lin,29,false

Generated JSON values are strings

[
  { "name": "Ada", "age": "36", "active": "true" },
  { "name": "Lin", "age": "29", "active": "false" }
]

A practical conversion checklist

  • Confirm that each JSON object represents one row in the intended table.
  • Decide how nested values should be handled before converting.
  • Check the CSV header order and inspect rows with missing properties.
  • When converting CSV back to JSON, remember that values remain strings.
  • Validate the resulting data in the application or spreadsheet that will consume it.

Related Tools

Your privacy matters

We use optional analytics cookies to understand how our tools are used and improve your experience. These controls apply to Google Analytics only, not advertising preferences. Privacy Policy