JSON Flatten & Unflatten — Convert Nested JSON to Flat Key Paths
Understanding the Basics
JSON flattening converts a deeply nested JSON object into a single-level object where each key is the full dot-notation path to the original value. For example, {"user": {"address": {"city": "London"}}} flattens to {"user.address.city": "London"}. This transformation has numerous practical applications. Elasticsearch and many NoSQL databases store documents as flat key-value maps internally, and understanding flat key paths is essential for writing effective query DSLs. Analytics platforms like BigQuery and Redshift perform better with flat structures than with nested JSONB columns. Data processing pipelines that transform JSON into CSV or Parquet benefit from a pre-flattening step. Flat representations also simplify JSON diffing, merge operations, and patch generation because every value has a unique unambiguous address. The inverse operation — unflattening — reconstructs the nested hierarchy from flat dot-notation keys, useful when reading back configuration stored in flat environment variables or key-value stores like Consul or etcd. Our tool implements both directions, correctly handling array indices (user.orders.0.id) and configurable separators (dot, slash, or underscore), giving developers precise control over flat data structures.
How to Use the Tool
- Select the operation: "Flatten" (nested → flat) or "Unflatten" (flat dot-keys → nested).
- Paste your JSON into the input editor.
- Choose the separator character: dot (.) is the default, but slash (/) and underscore (_) are also available.
- Click "Convert" — the output shows all values at a single depth level with composite key paths.
- Copy the flattened JSON for use in ETL pipelines, Elasticsearch mappings, or environment variable configs.
Key Features & Specifications
- Bidirectional: flatten nested objects to dot-path keys and unflatten them back
- Configurable separator: dot (.), slash (/), or custom character
- Correctly handles nested arrays with numeric indices (users.0.name)
- Preserves all value types (strings, numbers, booleans, null) at leaf nodes
- Useful for ETL pipelines, Elasticsearch ingestion, config systems, and JSON diff preparation
Frequently Asked Questions (FAQ)
- Q:How are arrays handled during flattening?
- Array elements are indexed numerically: an array at "items" with three elements produces keys "items.0", "items.1", and "items.2". During unflattening, numeric keys are recognized and reconstructed into arrays. You can configure if arrays should be left untouched or flattened.
- Q:What if my object keys contain dots?
- If your original key names contain the separator character (e.g., a key literally named "first.name"), flattening becomes ambiguous. The tool escapes such keys using bracket notation (first\.name). Choose a separator character not used in any key names to avoid this issue.
- Q:Can I use flattened JSON directly in MongoDB queries?
- Yes! MongoDB natively understands dot notation for querying nested documents. A flattened key like "address.city" maps directly to MongoDB's $match: {"address.city": "London"} query syntax.
- Q:Does flattening preserve the original data types?
- Yes. Leaf node types—strings, numbers, booleans, and null values—are preserved exactly. Only the structural layers (enclosing objects and arrays) are dismantled and converted into flattened path names.