Skip to content
ConvertMyStuff
Resource

Flattening Nested JSON for CSV

Nested JSON must be flattened to dot-notation or similar columns before most CSV and Excel workflows.

Developer ToolsRelated tool: Nested JSON to CSV Converter

Quick answer

Flattening turns nested keys like user.address.city into flat column names (user.address.city or user_address_city). Arrays may become indexed columns, multiple rows, or stringified cells depending on your converter settings and reporting grain.

Use the tool

Convert or calculate with our free nested json to csv converter.

Overview

CSV has no native concept of nesting. When JSON includes embedded objects, arrays of objects, or mixed structures, flattening defines how those values appear as columns—critical before spreadsheet import, BI ingestion, or email-friendly exports. This guide explains dot-notation conventions, array expansion strategies, and the edge cases that produce wide sparse spreadsheets or duplicated rows. Flattening is not a universal algorithm: the same JSON can become one row, many rows, or hundreds of columns depending on business rules you choose upfront with stakeholders.

Dot notation columns

The most common flattening strategy joins nested object keys with dots: { "user": { "name": "Sam", "role": "admin" } } becomes columns user.name and user.role with values Sam and admin. Some tools use underscores (user_name) or brackets (user[name]); pick one convention and keep it consistent across exports so Excel formulas and pivot tables remain stable.

Deep nesting—customer.address.geo.lat—can produce hundreds of columns when API schemas evolve. Preview column names before importing large files; wide CSVs with sparse data are hard to navigate in Excel and may exceed column limits in older tools. Consider projecting only the fields your report needs rather than flattening entire documents blindly. Document the projection list alongside the export job so the next analyst reproduces the same columns.

Handling arrays in flattened output

Arrays of primitives often stringify into a single cell—["red","blue"] becomes the text ["red","blue"] or red;blue depending on converter settings. Arrays of objects may expand to indexed columns (items.0.name, items.1.name), generate multiple CSV rows (one row per array element, duplicating parent fields), or require normalization into a separate related table.

Highly variable arrays—where each element has different keys—produce sparse CSV with many empty cells. An orders array where some line items include discount fields and others do not yields columns that are mostly blank. For analytics, normalizing into two CSV files (orders.csv and line_items.csv) linked by order_id is often cleaner than one impossibly wide sheet. Decide row grain before export: one row per order, per line item, or per shipment event.

Flattening strategies by use case

Reporting dashboards usually want one row per entity (one row per order, one row per user). Choose row-expansion when child arrays represent repeatable sub-records—line items, event logs, survey responses. Choose single-row flattening with indexed columns when array length is bounded and small (three phone numbers max).

Data warehouse loads often prefer JSON columns preserved in a lake and flattened in SQL with LATERAL FLATTEN or equivalent. For ad hoc Excel exports, browser-based flattening tools provide immediate preview without standing up ETL infrastructure. Match the strategy to how the consumer will filter and aggregate. When in doubt, ask whether the spreadsheet user expects one row per parent or one row per child event.

Edge cases and data quality

Null and missing keys behave differently: null may become an empty cell while a missing key omits the column entirely for that row. Mixed types in the same logical field—sometimes a string, sometimes a number—break pivot tables and SUM formulas after export.

Unicode, emoji, and newline characters inside string values require UTF-8 encoding and proper CSV quoting. Embedded commas and quotes in flattened values must be escaped per RFC 4180 or Excel misaligns columns. Always spot-check rows with special characters before sharing exports company-wide.

Flattening and reconstruction

Flattening is inherently lossy regarding structure unless you store enough metadata to rebuild nesting. CSV to JSON tools can infer object hierarchy from dot-notation column names, but array reconstruction requires consistent indexing (items.0, items.1) or separate files.

Do not expect CSV→JSON→CSV round trips to preserve original nesting without explicit structure rules documented alongside the export. For editable workflows, define a canonical flat schema and treat nested JSON as the system-of-record format, with CSV as a derived view.

Choosing flattening tools and settings

Browser-based nested JSON converters excel at ad hoc exports with live column preview—ideal for product managers validating API samples. Scriptable CLI flatteners (jq, custom Node scripts) fit CI pipelines that emit nightly CSV snapshots. Pick tools that expose array expansion mode explicitly rather than guessing defaults.

Before sharing flattened CSV company-wide, strip internal-only fields (internal_ids, cost_basis, PII) at flatten time. Wide exports make accidental oversharing easy because obscure nested keys become visible columns. Column allow-lists beat deny-lists when exporting customer data. Review a 50-row sample with legal or compliance before scheduling automated nightly flatten jobs.

Examples

  • Order with line items (row expansion)

    An order object with nested customer and an items array flattens to three CSV rows—one per line item—each repeating order_id and customer.email while varying product_name and quantity columns. Finance can sum quantity by SKU; support can filter by order_id without parsing JSON.

  • User profile with dot notation

    { "user": { "name": "Sam", "address": { "city": "Portland", "zip": "97201" } } } becomes columns user.name, user.address.city, and user.address.zip on a single row. CRM imports often expect this flat shape rather than nested JSON columns.

  • Variable-length tags array

    A product with tags ["sale", "featured"] becomes either a tags column with stringified array text or tags.0 and tags.1 columns; products with no tags leave those cells empty, creating sparsity in wide exports. Merchandising teams may prefer semicolon-delimited tags in one cell for simpler filters.

Common mistakes and edge cases

  • Expecting round-trip CSV→JSON→CSV to preserve original nesting without explicit structure rules.
  • Ignoring UTF-8 encoding when flattened data includes international characters or emoji.
  • Flattening entire API envelopes including meta and links objects, cluttering the spreadsheet with irrelevant columns.
  • Choosing indexed columns for unbounded arrays (hundreds of items), producing unusably wide CSV files.
  • Duplicating parent row data without a stable primary key column, making it impossible to recombine or deduplicate later.
  • Flattening personally identifiable nested fields into wide exports without redacting columns not needed by the recipient.

Related resources

Related tools

Last reviewed: 2026-05-23