·6 min read

How to Convert JSON to CSV (and Back)

JSON and CSV are two of the most common data formats. JSON dominates APIs and configuration, while CSV rules spreadsheets and data imports. Knowing how to move between them is a fundamental developer skill.

Why Convert Between JSON and CSV?

You pull data from an API and get JSON, but your client wants an Excel-compatible file. Or you receive a CSV export from a database and need to feed it into a web application that expects JSON. These conversions happen daily in development, data analysis, and business operations.

The quickest way is to use our free JSON to CSV converter — paste your data and get instant results. But understanding how the conversion works helps you handle edge cases.

JSON to CSV: The Basics

A JSON array of flat objects maps neatly to CSV. Each object becomes a row, and each key becomes a column header.

// Input JSON
[
  { "name": "Alice", "age": 30, "city": "NYC" },
  { "name": "Bob", "age": 25, "city": "LA" }
]

// Output CSV
name,age,city
Alice,30,NYC
Bob,25,LA

The algorithm is straightforward: collect all unique keys across all objects (some objects may have keys that others don't), write them as the header row, then iterate each object and output values in the same key order.

Handling Nested Objects

Real-world JSON is rarely flat. When objects contain nested objects or arrays, you need to flatten them. The most common approach is dot notation:

// Input
{ "name": "Alice", "address": { "city": "NYC", "zip": "10001" } }

// Flattened CSV headers
name,address.city,address.zip
Alice,NYC,10001

Arrays are trickier. You can either JSON-stringify them as a cell value, join them with a separator like |, or expand each array element into its own column (e.g., tags.0, tags.1).

CSV Edge Cases to Watch For

  • Commas in values: Wrap the field in double quotes. "New York, NY"
  • Quotes in values: Escape with double-double quotes. "She said ""hello"""
  • Newlines in values: Wrap in quotes. The CSV parser must handle multi-line quoted fields.
  • Missing keys: When objects have different shapes, output empty cells for missing keys.
  • Unicode: Ensure UTF-8 encoding. Add a BOM (\uFEFF) for Excel compatibility.

Code Examples

JavaScript

function jsonToCsv(data) {
  const headers = [...new Set(data.flatMap(Object.keys))];
  const rows = data.map(obj =>
    headers.map(h => {
      const val = obj[h] ?? "";
      const str = String(val);
      return str.includes(",") || str.includes('"')
        ? '"' + str.replace(/"/g, '""') + '"'
        : str;
    }).join(",")
  );
  return [headers.join(","), ...rows].join("\n");
}

Python

import csv, json, io

def json_to_csv(data):
    output = io.StringIO()
    writer = csv.DictWriter(output, fieldnames=data[0].keys())
    writer.writeheader()
    writer.writerows(data)
    return output.getvalue()

CSV to JSON: Going the Other Way

The reverse conversion parses CSV rows into objects. The first row provides the keys, and each subsequent row becomes an object with those keys.

// Input CSV
name,age,city
Alice,30,NYC

// Output JSON
[{ "name": "Alice", "age": "30", "city": "NYC" }]

Note that CSV doesn't preserve types — everything becomes a string. You may need to post-process to convert numbers and booleans back to their proper types.

When to Use Which Format

ScenarioBest Format
API responsesJSON
Spreadsheet import/exportCSV
Configuration filesJSON
Database bulk importsCSV
Nested/hierarchical dataJSON
Simple tabular dataCSV

Try It Now

Skip the code and convert your data instantly with our JSON to CSV converter. It handles nested objects, custom delimiters, and lets you download the result as a file — all without uploading your data to any server.

Try It Now — Free

Use our JSON to CSV Converter right in your browser. No signup, no upload to any server.

Open JSON to CSV Converter