JSON Flatten
Turn nested objects into a single level of dot-path keys, or reverse the operation.
Flatten Nested JSON Online
Convert a deeply nested JSON structure into a flat object with dot-notation keys. Useful for writing to spreadsheets, feeding data into flat key-value stores, simplifying Elasticsearch mappings, or debugging complex API responses. Paste your JSON above for instant results — or grab the code snippets below to flatten JSON inside your own JavaScript or Python project.
Example
{
"user": {
"name": "Alice",
"address": {
"city": "London",
"zip": "SW1A"
}
},
"scores": [10, 20, 30]
}{
"user.name": "Alice",
"user.address.city": "London",
"user.address.zip": "SW1A",
"scores.0": 10,
"scores.1": 20,
"scores.2": 30
}Flatten JSON in JavaScript
function flattenJson(obj, parentKey = '', sep = '.') {
return Object.entries(obj).reduce((acc, [key, value]) => {
const newKey = parentKey ? `${parentKey}${sep}${key}` : key;
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
Object.assign(acc, flattenJson(value, newKey, sep));
} else if (Array.isArray(value)) {
value.forEach((item, i) => {
const arrKey = `${newKey}${sep}${i}`;
if (typeof item === 'object' && item !== null) {
Object.assign(acc, flattenJson(item, arrKey, sep));
} else {
acc[arrKey] = item;
}
});
} else {
acc[newKey] = value;
}
return acc;
}, {});
}
// Usage
const nested = { user: { name: 'Alice', address: { city: 'London' } } };
const flat = flattenJson(nested);
// { 'user.name': 'Alice', 'user.address.city': 'London' }Flatten JSON in Python
def flatten_json(obj, parent_key='', sep='.'):
items = []
for k, v in obj.items():
new_key = f"{parent_key}{sep}{k}" if parent_key else k
if isinstance(v, dict):
items.extend(flatten_json(v, new_key, sep).items())
elif isinstance(v, list):
for i, item in enumerate(v):
arr_key = f"{new_key}{sep}{i}"
if isinstance(item, dict):
items.extend(flatten_json(item, arr_key, sep).items())
else:
items.append((arr_key, item))
else:
items.append((new_key, v))
return dict(items)
# Usage
import json
nested = {"user": {"name": "Alice", "address": {"city": "London"}}}
flat = flatten_json(nested)
# {'user.name': 'Alice', 'user.address.city': 'London'}When to Flatten JSON
How to Use the Tool
- Paste your nested JSON into the input panel above
- Choose your separator (default is
.for dot notation) - Click Flatten to get the single-level output
- Copy the result or download as
.json
Frequently Asked Questions
How do I flatten nested JSON in JavaScript?
You can recursively walk objects and build flat keys such as user.name or order.items[0].price, or use this page's tool instantly in the browser. For unflatten and separator options, see JSON Flatten.
Why flatten nested API responses?
Flattening makes deeply nested API data easier to display, export, search, or compare—pair with the JSON Formatter when inspecting payloads.
How are arrays handled when flattening?
Arrays are often converted using indexes in the generated keys—for example scores.0, scores.1 in the sample above.
Can I flatten JSON in Python too?
Yes. Similar recursive logic can be used in Python data workflows—see the Python snippet on this page.
What separator is best for flattened keys?
Dot notation is common, but some workflows use underscores or custom separators. JSON Flatten on this site supports dot, underscore, and slash.
Can I export flattened output to CSV?
Yes. Flattened JSON is often ideal for CSV export—use JSON to CSV next.
Related Tools