Flatten JSON in JavaScript

Preview how nested JavaScript objects become flat path maps, then copy the result into fixtures, frontend tests, Node scripts, or API debugging notes.

Paste your JSON → Get results instantly (no signup)

⚡ Instant resultsNo signupRuns in your browser
Try examples:

Flatten or nest keys in this JSON structure and flatten nested object.

{
"user": {
"name": "Alice",
"address": {
"city": "NYC",
"zip": "10001"
}
}
}
Output
1{
2 "user.name": "Alice",
3 "user.address.city": "NYC",
4 "user.address.zip": "10001"
5}

Love the result?

Use this exact pipeline in your app, backend, or LLM workflow.

No setup needed. Works with curl, Node, Python.

Uses example data. For edited input, copy from the playground.

Read integration guide

Works with:

  • JavaScript objects
  • Node.js scripts
  • Frontend fixtures
  • API response debugging

Example: input → output

Flatten JSON in JavaScript

JavaScript flatten JSON searches usually come from a practical problem: a nested object is hard to compare, log, test, or map into a table. You may have an API response in a React component, a Node.js script preparing fixtures, or a test snapshot where user.profile.email is easier to assert than response.user.profile.email.

This page lets you preview the flattened output before writing a helper. It uses the same engine as the Flatten JSON tool, with examples focused on JavaScript object paths and array behavior.

JavaScript flatten JSON example

Input object:

const input = {
  user: {
    id: 7,
    profile: {
      email: "ada@example.com",
      active: true
    }
  }
};

Flat output:

{
  "user.id": 7,
  "user.profile.email": "ada@example.com",
  "user.profile.active": true
}

That shape is convenient for assertions, logs, CSV headers, and simple key-value inspection.

A simple JavaScript flatten helper

function flattenObject(value, prefix = "", output = {}) {
  if (value === null || typeof value !== "object" || Array.isArray(value)) {
    output[prefix] = value;
    return output;
  }

  for (const [key, child] of Object.entries(value)) {
    const path = prefix ? `${prefix}.${key}` : key;
    flattenObject(child, path, output);
  }

  return output;
}

That snippet keeps arrays intact. If you need indexed paths such as items.0.sku, handle arrays separately and recurse through each index. Preview that behavior with Flatten JSON Array before adding it to production code.

JavaScript edge cases

  • null is a JSON value, not an object to recurse into.
  • Arrays may need to stay arrays for APIs but expand for CSV.
  • Dotted source keys can collide with dotted path output.
  • Date, Map, Set, and class instances are not JSON values until serialized.
  • Property order is usually not a durable contract for downstream systems.

Online tool vs a JS library

Use the online flattener when you need to inspect a payload quickly, create a fixture, debug a webhook, or confirm a path convention. Use a JavaScript library when flattening is part of application logic, batch processing, or a production API.

The online page is still useful before installing a package because it makes the expected output concrete. Once you know the delimiter, array mode, and null behavior, the implementation choice is much less ambiguous.

Next JavaScript workflows

After flattening, export with JSON to CSV, compare payloads with JSON Diff, or rebuild nested structure with Unflatten JSON. For generic examples, return to the main JSON flattener.

Frequently asked questions

How do I flatten JSON in JavaScript?+

Walk the object recursively, join each key to the current path, and emit primitive values at the leaves. Handle null and arrays deliberately because they are common edge cases.

Should JavaScript flatten arrays by index?+

Only when the destination expects indexed paths such as items.0.sku. Keep arrays intact when app code or an API still expects arrays.

Why use the online tool if I will write JavaScript?+

It helps you preview the expected output, delimiter, array behavior, and null handling before choosing a helper function or package.

Related tools

Read more on the blog

Advanced usage (optional)

Flatten / Nest

v1.0.0
Structure
objectarrayreversible

Description

Flatten / Nest

Convert between nested and flat object structures. Flatten deep objects into single-level key-value pairs, or nest flat keys back into hierarchical objects. Supports multiple key formats: delimiter-separated, camelCase, snake_case, PascalCase, and kebab-case.

Modes

Flatten

Convert nested objects into flat keys. Each nested path becomes part of the key name.

{ "user": { "name": "Alice" } }  →  { "user.name": "Alice" }

Nest

Convert flat keys back into nested objects by splitting on the delimiter or case boundaries.

{ "user.name": "Alice" }  →  { "user": { "name": "Alice" } }

Key Formats

Delimiter (default)

Join/split key segments with a character (default: .).

  • Flatten: user + nameuser.name
  • Nest: user.nameuser / name

camelCase

Join/split on uppercase letter boundaries.

  • Flatten: user + address + cityuserAddressCity
  • Nest: userAddressCityuser / address / city

snake_case

Join/split on underscores.

  • Flatten: user + address + cityuser_address_city
  • Nest: user_address_cityuser / address / city

PascalCase

Same as camelCase but with uppercase first letter.

  • Flatten: user + nameUserName

kebab-case

Join/split on hyphens.

  • Flatten: user + nameuser-name

Configuration

FieldTypeDefaultDescription
Modeenumflattenflatten or nest
Key Formatenumdelimiterdelimiter, camelCase, snake_case, PascalCase, or kebab-case
Delimiterstring.Character(s) used to join/split key segments (only for delimiter format)
Max Depthnumber0Maximum nesting depth to flatten (0 = unlimited)
Target Pathspath-picker[]Scope operation to specific paths only (empty = apply everywhere)

Use Cases

API Integration

  • Flatten for forms: Convert nested API responses to flat form field names
  • Nest for APIs: Convert flat form data back to nested API request bodies
  • Format conversion: Transform between dot-notation and camelCase conventions

Database Operations

  • MongoDB flattening: Flatten nested documents for tabular export
  • SQL mapping: Convert hierarchical JSON to flat column names for SQL insertion
  • Schema migration: Convert between naming conventions (snake_case ↔ camelCase)

Configuration Management

  • Environment variables: Flatten config objects to dot-notation for .env files
  • Depth limiting: Flatten only the first level while preserving deep structures

Configuration

NameTypeDefaultDescription
Modeenumflattenflatten: convert nested objects to flat keys. nest: convert flat keys back to nested objects. flatten nest
Key Formatenumdelimiterdelimiter: use a character to join/split. Others: camelCase, snake_case, PascalCase, kebab-case. delimiter camelCase snake_case PascalCase kebab-case
Delimiterstring.Character(s) used to join/split key segments (default is dot)
Max Depthnumber0Maximum nesting depth to flatten (0 = unlimited)
Target Pathspath-picker[]Scope operation to specific paths only (empty = apply everywhere)

Examples

AI Prompt
Flatten or nest keys in this JSON structure and flatten nested object.
{
"user": {
"name": "Alice",
"address": {
"city": "NYC",
"zip": "10001"
}
}
}
Output
1{
2 "user.name": "Alice",
3 "user.address.city": "NYC",
4 "user.address.zip": "10001"
5}
Config
Mode
flatten
Delimiter
.
Max Depth
0
Target Paths
all

API Usage

POST /api/v1/utilities/structure.flatten-nest
Example:
curl -X POST https://your-domain.com/api/v1/utilities/structure.flatten-nest \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"inputs":{"primary":{"user":{"name":"Alice","address":{"city":"NYC","zip":"10001"}}}},"config":{"mode":"flatten","delimiter":".","maxDepth":0,"targetPaths":[]}}'
Response
1{
2 "user.name": "Alice",
3 "user.address.city": "NYC",
4 "user.address.zip": "10001"
5}