JSON to CSV Conversion: Handling Nested Objects, Arrays, and Edge Cases

●2 ●92 ●140
calendar_today • schedule2 min read

JSON is the default API response format. CSV is what spreadsheets, databases, and data pipelines want. Converting between them is routine — but nested JSON makes it less simple than it first appears.

The Simple Case: Flat Arrays

If your JSON is a flat array of objects with uniform keys, conversion is trivial:

[
  {"id": 1, "name": "Alice", "role": "engineer"},
  {"id": 2, "name": "Bob", "role": "designer"}
]

Becomes:

id,name,role
1,Alice,engineer
2,Bob,designer

Every JSON-to-CSV tool handles this case. The challenge is what comes next.

Nested Objects: Flattening with Dot Notation

Real API responses usually have nested objects:

[{"id": 1, "user": {"name": "Alice", "email": "*Emails are not allowed*"}, "total": 99.99}]

The standard approach is dot-notation flattening: user.name, user.email become column headers.

id,user.name,user.email,total
1,Alice,*Emails are not allowed*,99.99

This preserves the data and works well with most analysis tools.

Arrays Inside Objects

When a field contains an array, you have three choices:

  1. Join — tags: ["a","b","c"] → "a,b,c" in one cell (most common)
  2. Expand — Create one row per array item (right for one-to-many relationships)
  3. Index — tags[0], tags[1]... as columns (breaks when lengths vary)

Joining works for most cases. Expanding is correct for structured one-to-many data (e.g., order → line items).

CSV Quoting Rules

A common mistake is forgetting that CSV has strict quoting requirements:

  • Values containing commas must be quoted: "New York, NY"
  • Quotes inside quoted values must be doubled: "He said ""hello"""
  • Newlines inside values must be quoted
  • The first row is the header by convention

Naive concatenation with commas will break on real-world data.

JavaScript Implementation

function flatten(obj, prefix = '') {
  return Object.keys(obj).reduce((acc, key) => {
    const fullKey = prefix ? `${prefix}.${key}` : key;
    const val = obj[key];
    if (typeof val === 'object' && !Array.isArray(val) && val !== null) {
      Object.assign(acc, flatten(val, fullKey));
    } else {
      acc[fullKey] = Array.isArray(val) ? val.join(', ') : val;
    }
    return acc;
  }, {});
}

function jsonToCSV(data) {
  const flat = data.map(row => flatten(row));
  const headers = [...new Set(flat.flatMap(Object.keys))];
  const escape = v => {
    const s = String(v ?? '');
    return s.includes(',') || s.includes('"') || s.includes('\n')
      ? `"${s.replace(/"/g, '""')}"` : s;
  };
  return [
    headers.join(','),
    ...flat.map(row => headers.map(h => escape(row[h] ?? '')).join(','))
  ].join('\n');
}

Python Implementation

import csv, json

def flatten(obj, prefix=''):
    out = {}
    for k, v in obj.items():
        key = f"{prefix}.{k}" if prefix else k
        if isinstance(v, dict):
            out.update(flatten(v, key))
        elif isinstance(v, list):
            out[key] = ', '.join(str(x) for x in v)
        else:
            out[key] = v
    return out

data = json.load(open('input.json'))
flat = [flatten(row) for row in data]
headers = list(dict.fromkeys(k for row in flat for k in row))

with open('output.csv', 'w', newline='') as f:
    w = csv.DictWriter(f, fieldnames=headers, extrasaction='ignore')
    w.writeheader()
    w.writerows(flat)

Quick Conversions in the Browser

For one-off jobs — converting an API response you've copied, or a file from a colleague — writing code is overkill. SnappyTools' JSON to CSV converter handles flat and nested JSON, offers dot-notation flattening, and downloads the result as .csv. No data is uploaded; processing is client-side.

Summary

  • Flat JSON → CSV: trivial, any tool handles it
  • Nested objects: flatten with dot notation (most compatible)
  • Arrays in objects: join with comma (simplest) or expand to rows (for one-to-many)
  • Always quote values with commas, quotes, or newlines
  • Browser tool for one-off use: snappytools.app/json-to-csv-converter/

Originally published at https://snappytools.app/json-to-csv-converter/

Want more #webdev & #javascript deep dives?

Join 6,129+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

Free SEO Tools Every Small Business Should Use (2026) | FloxoLab

floxolab - Oct 20

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelski - Mar 19

What Is SARIF and How Does It Help Security Tools Work Together?

Ganesh Kumar - Jul 4

ERC20 Edge Cases Every Smart Contract Engineer Should Know

BinnaDev - Jun 24

CSV to JSON: The Complete Conversion Guide for Developers

SnappyTools - May 30
chevron_left
2.5k Points • 234 Badges
102Posts
0Comments
SnappyTools builds free, fast, browser-based tools for developers, writers, and designers. No signup... Show more

Related Jobs

Commenters (This Week)

4 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!