Skip to content

How to Convert JSON to CSV: Methods, Tools and Best Practices

JSON is the dominant format for APIs, configuration files and data interchange. But when it comes to data analysis, spreadsheet imports or reporting, CSV is often the format you actually need. Converting between the two is straightforward for simple data, but nested structures and inconsistent schemas can make it surprisingly tricky. This guide walks through the key concepts, common pitfalls and best practices for JSON to CSV conversion.

Structure Differences: Hierarchical vs Tabular

JSON and CSV represent data in fundamentally different ways. JSON is hierarchical. It supports nested objects, arrays and mixed types at any depth. CSV is flat and tabular. Every row has the same columns, and every cell holds a single value.

A simple JSON array of objects maps cleanly to CSV rows. Each object becomes a row, and each key becomes a column header. The challenge begins when your JSON contains nested objects or arrays, because those structures have no direct equivalent in a flat table.

Converting Flat JSON

Flat JSON is the simplest case. When your data is an array of objects with only primitive values (strings, numbers, booleans), the conversion is direct:

// Input JSON
[
  { "id": 1, "name": "Alice", "email": "alice@example.com" },
  { "id": 2, "name": "Bob", "email": "bob@example.com" },
  { "id": 3, "name": "Charlie", "email": "charlie@example.com" }
]

This converts cleanly to:

id,name,email
1,Alice,alice@example.com
2,Bob,bob@example.com
3,Charlie,charlie@example.com

The keys from the first object become the CSV header row. Each subsequent object fills in a data row. If all objects share the same keys, the conversion is lossless.

Handling Nested Objects

When JSON contains nested objects, you need a flattening strategy. The most common approach is dot notation, where nested keys are joined with a period to create a flat column name:

// Input JSON
[
  {
    "id": 1,
    "name": "Alice",
    "address": {
      "city": "New York",
      "country": "US"
    }
  }
]

// Flattened CSV output
id,name,address.city,address.country
1,Alice,New York,US

This approach works well for one or two levels of nesting. For deeply nested structures, the column names can become long and unwieldy. In those cases, you may want to selectively flatten only the fields you need, or serialize deeper objects as JSON strings within the CSV cell.

Handling Arrays

Arrays in JSON are especially challenging because a single field can hold multiple values. There are several common strategies for handling them:

  • Join as a delimited string. Combine array values with a separator like a semicolon or pipe character. For example, ["admin", "editor"] becomes admin;editor.
  • Create indexed columns. Generate separate columns for each array index, like roles.0 and roles.1. This works when arrays have a fixed or predictable length.
  • Expand into multiple rows. Create one row per array element, duplicating the parent fields. This is useful for relational analysis but increases row count significantly.
  • Serialize as JSON. Keep the array as a JSON string in the CSV cell. This preserves the data but is harder to work with in spreadsheets.

Common Pitfalls

Several issues can trip you up when converting JSON to CSV:

  • Commas in values. If a string value contains a comma, it must be wrapped in double quotes in CSV. Otherwise the comma will be interpreted as a column separator, corrupting the row structure.
  • Null and undefined values. JSON supports null as a value, but CSV has no standard representation for it. Decide whether to output an empty string, the literal text "null" or another placeholder.
  • Inconsistent keys. Not all objects in a JSON array may have the same keys. Your converter needs to collect all unique keys across all objects to build the full header row, and fill in empty values for missing fields.
  • Special characters. Newlines, double quotes and Unicode characters within values all need proper escaping. Double quotes inside a quoted CSV field must be escaped by doubling them.
  • Large numbers. Spreadsheet applications like Excel may silently truncate or round large numbers. Long numeric IDs can lose precision when opened in a spreadsheet.

Use Cases

JSON to CSV conversion comes up in many practical scenarios:

  • Data analysis. Export API data to CSV so you can analyze it in Excel, Google Sheets or tools like pandas and R.
  • Spreadsheet import. Many business tools and CRMs only accept CSV uploads. Converting your JSON export to CSV is a necessary step.
  • Reporting. Generate CSV reports from JSON data for stakeholders who are more comfortable with tabular formats.
  • Database migration. Bulk import data into SQL databases using CSV, which most database tools support natively for import.
  • Data sharing. CSV is a universal format that anyone can open without specialized software. It is the lowest common denominator for data exchange.

Best Practices

  • Validate your JSON first. Make sure the input is valid JSON before attempting conversion. A missing bracket or trailing comma will cause the entire conversion to fail.
  • Handle encoding correctly. Use UTF-8 encoding for your CSV output. If your data contains non-ASCII characters, add a BOM (byte order mark) to help Excel detect the encoding properly.
  • Choose your delimiter wisely. While commas are standard, semicolons or tabs may be better choices if your data frequently contains commas. Many tools support custom delimiters.
  • Test with edge cases. Try empty arrays, null values, deeply nested objects, special characters and very large datasets. These edge cases reveal bugs that simple test data will not catch.
  • Flatten only what you need. For deeply nested JSON, selectively extract the fields you care about rather than trying to flatten everything. This keeps the output clean and manageable.
  • Preserve data types when possible. Consider whether numeric strings should remain as strings or be converted to numbers. Leading zeros in fields like ZIP codes will be lost if treated as numbers.

Try it yourself

Paste any JSON array and instantly convert it to CSV. Handles nested objects, arrays and special characters. Runs entirely in your browser.

Open JSON to CSV Converter