ZenovayTools

JSON Flatten / Unflatten

Flatten nested JSON objects into dot-notation keys (e.g., user.address.city) and unflatten them back. Configurable separator and array handling.

Flattened Output

12 keys
{
  "user.id": 42,
  "user.name": "Alice",
  "user.roles.0": "admin",
  "user.roles.1": "editor",
  "user.address.city": "Berlin",
  "user.address.country": "Germany",
  "user.address.zip": "10115",
  "user.preferences.theme": "dark",
  "user.preferences.notifications.email": true,
  "user.preferences.notifications.sms": false,
  "meta.version": "1.0",
  "meta.created": "2024-01-15"
}

How to Use JSON Flatten / Unflatten

  1. 1Paste a nested JSON object into the input.
  2. 2Choose Flatten to convert to dot-notation keys, or Unflatten to convert back.
  3. 3Configure the separator character and copy the result.
Zenovay

Track your website performance

Real-time analytics, session replay, heatmaps, and AI insights. 2-minute setup, privacy-first.

Try Zenovay Analytics — Free

Frequently Asked Questions

What is JSON flattening and when is it useful?
JSON flattening converts nested objects into a single-level object where nested keys are joined with a separator (usually dot). Example: {"user": {"name": "Alice", "address": {"city": "Berlin"}}} → {"user.name": "Alice", "user.address.city": "Berlin"}. Use cases: flattening for database column storage (NoSQL to SQL migration), simplifying deeply nested API responses, feeding into machine learning pipelines that expect tabular data, Elasticsearch document indexing, and sending analytics events with flat properties.
How does array flattening work?
Arrays are flattened using their index as part of the key. Example: {"tags": ["js", "ts"]} → {"tags.0": "js", "tags.1": "ts"} with dot separator, or {"tags[0]": "js", "tags[1]": "ts"} with bracket notation. Nested arrays: {"matrix": [[1,2],[3,4]]} → {"matrix.0.0": 1, "matrix.0.1": 2, "matrix.1.0": 3, "matrix.1.1": 4}. Reconstructing arrays on unflattening requires knowing which keys are numeric indices. Some tools preserve array structure during unflattening; others convert to objects.
What separators are commonly used for flattened keys?
Dot (.): most common, intuitive, conflicts with keys that contain dots. Underscore (_): avoids conflicts with dots, used in some database column naming conventions. Double underscore (__): avoids conflicts with single underscores, used in Django environment variables and Helm chart values. Slash (/): used in JSON Pointer (RFC 6901) — "/user/address/city". Colon (:): used in some config systems. Best practice: choose a separator that doesn't appear in your existing key names. Avoid dots if keys might contain package names or domain names.
What is JSON Pointer (RFC 6901)?
JSON Pointer is an IETF standard (RFC 6901) for referencing a specific value within a JSON document. Format: /key/subkey/index. Root document: "". First element of array: "/0". Nested value: "/user/address/city". Special characters: "~0" represents "~", "~1" represents "/". Used in: JSON Patch (RFC 6902) for partial updates, JSON Schema $ref, OpenAPI path references. Example: PATCH with {"op": "replace", "path": "/user/address/city", "value": "London"} changes a nested value without replacing the entire document.
How do I flatten JSON in JavaScript without a library?
function flatten(obj, prefix = "", sep = ".") { return Object.entries(obj).reduce((acc, [key, val]) => { const newKey = prefix ? prefix + sep + key : key; if (val && typeof val === "object" && !Array.isArray(val)) { Object.assign(acc, flatten(val, newKey, sep)); } else if (Array.isArray(val)) { val.forEach((item, i) => { if (item && typeof item === "object") { Object.assign(acc, flatten(item, newKey + sep + i, sep)); } else { acc[newKey + sep + i] = item; } }); } else { acc[newKey] = val; } return acc; }, {}); }. For the inverse (unflatten), split each key on the separator and rebuild the nested structure.