← All tools JSON

Flatten JSON

Convert nested JSON objects into flat key-value pairs with dot or bracket notation, or unflatten them back. Everything runs in your browser — nothing is sent to a server.

100% client-side

All processing runs entirely in your browser. Your JSON data is never sent to any server.

Why Flatten JSON? Use Cases for Databases and Logging

Modern APIs and NoSQL databases produce deeply nested JSON. While nesting is natural for representing hierarchical data, many downstream systems — relational databases, CSV exports, logging platforms, and analytics tools — work best with flat, single-level structures. JSON flattening bridges that gap by converting nested keys into path-style identifiers.

For example, a user object with nested address data becomes a set of simple key-value pairs like user.address.city, ready for insertion into a database column or a log field.

Dot Notation vs. Bracket Notation

. Dot Notation
  • • Most common and readable format
  • user.address.city
  • • Used in JavaScript, MongoDB, Elasticsearch
  • • Arrays: tags[0]
[ ] Bracket Notation
  • • Used in form encoding and PHP
  • user[address][city]
  • • Compatible with query strings
  • • Arrays: tags[0]

Common Scenarios for JSON Flattening

  1. 1

    Relational database import. SQL tables have fixed columns. Flattening nested JSON creates one column per path, making it straightforward to map fields to table schemas for bulk imports.

  2. 2

    Log aggregation and search. Platforms like Elasticsearch, Datadog, and Splunk index flat fields. Flattened keys allow you to search and filter on deeply nested values directly.

  3. 3

    CSV and spreadsheet export. Flat JSON maps directly to CSV columns. Each flattened key becomes a header, and each value fills a cell — no nested data to lose.

  4. 4

    Configuration diffing. Comparing two flat configs line-by-line is far simpler than comparing nested trees. Flattening reveals every changed value at a glance.

  5. 5

    Analytics pipelines. Tools like BigQuery and Redshift work best with flat schemas. Pre-flattening API payloads before ingestion simplifies queries and reduces transformation logic.

How the Flatten Algorithm Works

The flattening algorithm recursively traverses the JSON tree. For each node, it builds a path string by concatenating parent keys with the chosen separator. When it reaches a leaf value (string, number, boolean, or null), it stores the full path as the key and the leaf as the value. Arrays are handled by appending the index in bracket notation regardless of the separator choice.

Unflattening reverses this process: it splits each key on dots and bracket indices, then reconstructs the nested structure one segment at a time. Numeric segments trigger array creation, while string segments create objects.

Frequently Asked Questions

What does it mean to flatten JSON?

Flattening JSON converts a deeply nested object into a single-level object where each key represents the full path to a value using dot notation or bracket notation. This makes nested data easier to store in flat structures like spreadsheets, relational databases, or log systems.

How are arrays handled when flattening JSON?

Arrays are flattened using index-based bracket notation. For example, an array containing two items becomes tags[0] and tags[1]. Nested objects inside arrays are further expanded with additional path segments.

What is the difference between dot notation and bracket notation?

Dot notation uses periods as separators and is the most common and readable format. Bracket notation wraps each key in square brackets and is often used in form data encoding and PHP-style parameters. Both produce equivalent flat representations of the same data.

Can I convert flattened JSON back to nested JSON?

Yes. Switch to "Unflatten" mode and paste a flat JSON object with dot or bracket notation keys. The tool will reconstruct the original nested structure, including arrays, by parsing the path segments in each key.

When should I flatten JSON in production?

Flatten JSON when importing data into relational databases or CSV files, indexing nested fields in search engines, sending structured data to logging platforms, building flat configuration files, or transforming API responses for analytics pipelines.

Related Tools