Home / Blog

Convert JSON to CSV (and Back)

JSON and CSV conversions are common in analytics, imports/exports, and reporting pipelines. This guide shows reliable conversion patterns, pitfalls with nested structures, and how to validate output before downstream use.

When to Use JSON vs CSV

  • JSON: hierarchical, API-native, supports nested objects/arrays.
  • CSV: tabular, spreadsheet-friendly, ideal for BI workflows.

Use JSON to CSV when handing data to analysts, and CSV to JSON when moving data back into services.

Core Mapping Rules

JSON conceptCSV mappingRisk
Object keysColumnsMissing keys produce blank values
Array of objectsRowsInconsistent row schema
Nested objectFlatten or stringifyLoss of structure

JSON to CSV Example

[
  {"id":1,"name":"Ava","city":"Austin"},
  {"id":2,"name":"Liam","city":"Denver"}
]

Output:

id,name,city
1,Ava,Austin
2,Liam,Denver

CSV to JSON Example

id,name,city
1,Ava,Austin
2,Liam,Denver

Output:

[
  {"id":"1","name":"Ava","city":"Austin"},
  {"id":"2","name":"Liam","city":"Denver"}
]

Code Snippets by Language

JavaScript

const rows = data.map(r => [r.id, r.name, r.city].join(','));
const csv = ['id,name,city', ...rows].join('
');

Python

import csv, json
with open('input.json') as f:
    data = json.load(f)
with open('out.csv', 'w', newline='') as f:
    w = csv.DictWriter(f, fieldnames=['id','name','city'])
    w.writeheader(); w.writerows(data)

C#

// Use CsvHelper + System.Text.Json for robust conversion in production

Java

// Use Jackson for JSON + OpenCSV for CSV serialization

Real Use Cases

  • Export API response to spreadsheet for stakeholder review.
  • Import CSV from operations team into backend service.
  • Data migration where source is CSV and target contract is JSON.

Validation Checklist After Conversion

  1. Validate JSON output using JSON Validator.
  2. Reformat with JSON Formatter for visual inspection.
  3. Compare pre/post records with JSON Diff.
  4. Watch for type coercion (numbers become strings in CSV pipelines).

Screenshot Recommendations

  • JSON array input and CSV output side-by-side.
  • CSV input converted back to JSON and validated.
  • Diff screenshot showing changed types after roundtrip conversion.

Pro Tips

  • Flatten nested JSON before CSV export to preserve tabular clarity.
  • Document column-to-key mappings in ETL jobs.
  • Normalize null/empty values before conversion.

FAQ

Can I convert nested JSON directly to CSV?

Yes, but nested data should be flattened or stringified to avoid ambiguity.

Why are numbers quoted after CSV to JSON conversion?

CSV has no native typing; parsers often default to strings unless you cast values explicitly.

What tool should I use for fast conversion?

Use JSON to CSV and CSV to JSON on formatterjson.org.

Get Started with formatterjson.org

Convert with JSON to CSV and CSV to JSON, then validate using JSON Validator.