JSON Flatten / Unflatten
JSON Flatten / Unflatten Tool
The JSON Flatten / Unflatten tool converts deeply nested JSON into a flat, single-level object using dot-notated keys. This is especially useful for analytics, CSV exports, spreadsheets, and database inserts where nested structures are hard to work with. When you are done, you can unflatten the data back into the original nested structure with a single click. Everything runs locally in your browser.
Flattening makes JSON easier to search, filter, and compare. It also reduces complexity when transforming JSON into tabular formats such as CSV or SQL. Unflattening is the reverse operation: it rebuilds nested objects from flat keys, preserving structure so you can re-use the JSON in APIs or applications.
What flattening does
- Turns nested objects into a single-level object with compound keys.
- Uses a delimiter (such as a dot) to represent nested paths.
- Preserves values without changing data types.
- Makes data compatible with CSV exports and columnar databases.
How to use the JSON Flatten tool
- Paste JSON in the left editor.
- Choose a delimiter (dot, underscore, or custom).
- Click Flatten to produce flat output.
- Use Unflatten to reconstruct the nested structure.
Example: flatten JSON
Input JSON:
{
"user": {
"id": 10,
"name": "Avi",
"profile": {
"email": "avi@example.com",
"active": true
}
},
"meta": {
"source": "api",
"version": 2
}
}
Flattened output (dot delimiter):
{
"user.id": 10,
"user.name": "Avi",
"user.profile.email": "avi@example.com",
"user.profile.active": true,
"meta.source": "api",
"meta.version": 2
}
Example: unflatten JSON
Input (flat JSON):
{
"order.id": 501,
"order.total": 99.5,
"order.items.0.name": "Pen",
"order.items.0.qty": 2
}
Unflattened output:
{
"order": {
"id": 501,
"total": 99.5,
"items": [
{ "name": "Pen", "qty": 2 }
]
}
}
Handling arrays when flattening
Arrays are flattened using numeric indices in the key path. For example, items[0].name becomes items.0.name with a dot delimiter. When you unflatten, those numeric keys are reconstructed into arrays. If your data uses numeric keys intentionally (not as array indexes), consider using a different delimiter or pre-processing the input.
Common errors and solutions
- Invalid JSON: Flattening requires valid JSON. Use JSON Validator to fix syntax errors first.
- Delimiter conflicts: If your keys already contain the delimiter (e.g., a dot in key names), flattening may create ambiguous paths. Choose a different delimiter like
__or|. - Unexpected arrays on unflatten: Numeric keys are treated as array indexes. If you want objects instead, rename those keys or use a different delimiter and mapping strategy.
- Data loss due to collisions: Two different nested paths might produce the same flat key when the delimiter is inconsistent. Use a unique delimiter and avoid keys that collide.
- Large JSON performance: Very large inputs can slow down the browser. Use smaller samples or split the data into sections.
Best practices for flattening JSON
- Choose a delimiter that does not appear in your original keys.
- Keep a copy of the original JSON before flattening for safe comparison.
- Validate the flattened output when exporting to CSV or SQL.
- Use JSON to CSV after flattening for clean spreadsheets.
- Use JSON Compare to verify unflatten results match the original.
Delimiter strategy and key safety
Delimiter choice is the most important decision when flattening. A dot is common, but it can create ambiguity if your original keys already contain dots. For example, a key like meta.version will be indistinguishable from a nested meta object. In those cases, use a safer delimiter like __ or |. You can also pre-process keys to replace dots with another character before flattening.
If you plan to unflatten later, keep the delimiter consistent. Changing delimiters between flatten and unflatten can break paths and produce unexpected results. A good practice is to choose a delimiter once for your pipeline and document it.
Flattening for CSV and SQL
Flattened JSON is ideal for tabular formats. Each flattened key becomes a column name, which maps cleanly to CSV headers or SQL columns. Before exporting, review long keys and consider renaming them to shorter, readable names with JSON Key Renamer. This makes CSV files easier to consume and SQL schemas more manageable.
When flattening large objects, consider selecting only the fields you need with JSON Remove Keys. This reduces column count and keeps exports focused on relevant data.
Edge cases to watch
- Mixed arrays: Arrays containing different object shapes may flatten into uneven columns.
- Null values: Nulls are preserved, but downstream tools may treat them differently.
- Empty objects: Empty nested objects may disappear after flattening if they contain no fields.
- Key collisions: Two different nested paths may flatten into the same key if the delimiter conflicts.
Practical workflow
- Validate JSON to confirm the input is clean.
- Pick a safe delimiter and flatten the JSON.
- Review the keys for readability and rename if needed.
- Export to CSV or SQL, or unflatten to verify round-trip integrity.
Use cases
Analytics: Flatten nested API responses to create a dataset where each key becomes a column.
Data exports: Flatten JSON before converting to CSV or SQL inserts.
Debugging: A flat view makes it easier to search and compare objects.
Data pipelines: Many ETL tools expect flat data structures.
FAQs
Does flattening change my values?
No. Flattening only changes the structure of the keys, not the values.
Can I unflatten any flat JSON?
Yes, as long as the key paths are consistent and use the selected delimiter.
How are arrays handled?
Arrays are flattened using numeric indexes. Unflattening rebuilds arrays from those indexes.
Is my JSON uploaded?
No. All conversions happen locally in your browser.
What delimiter should I use?
Use a delimiter that does not appear in your keys. Dot is common, but double underscore or pipe can be safer.
Can I flatten JSON for SQL inserts?
Yes. Flatten first, then use JSON to SQL to generate insert statements.
How do I handle keys with dots?
Choose a different delimiter or pre-process keys to replace dots before flattening.
Will flattening preserve order?
Object key order is preserved as much as possible, but JSON objects are unordered by definition.
Does flattening work for deeply nested JSON?
Yes. The tool flattens any depth, but long keys may be harder to read. Consider renaming keys afterward.
Can I flatten data for analytics dashboards?
Yes. Flattened JSON maps cleanly to columns, which is ideal for analytics tools.
How do I reduce columns after flattening?
Remove unwanted keys with JSON Remove Keys or use a keep list.
Keyword‑targeted phrases
- flatten json online
- unflatten json
- json to dot notation
- flatten nested json
- unflatten dot keys
Related tools: JSON to CSV, CSV to JSON, JSON Formatter, JSON to SQL