Free Online JSON Flattener & Unflatten Tool
Transform nested JSON into flat key-value pairs or restore flattened JSON back to nested structure. Real-time processing with customizable delimiters, array handling, and bidirectional conversion. Perfect for CSV exports, database storage, and data analysis.
Want to test drive the tool?
Before & After Example
{
"user": {
"name": "Alice Johnson",
"email": "alice@example.com",
"address": {
"street": "123 Main St",
"city": "Portland",
"zip": "97201"
},
"tags": ["admin", "editor"]
}
}{
"user.name": "Alice Johnson",
"user.email": "alice@example.com",
"user.address.street": "123 Main St",
"user.address.city": "Portland",
"user.address.zip": "97201",
"user.tags[0]": "admin",
"user.tags[1]": "editor"
}Delimiter Options
| Delimiter | Nested Path | Flattened Key | Best For |
|---|---|---|---|
| Dot (.) | user > address > city | user.address.city | JavaScript / general use |
| Underscore (_) | user > address > city | user_address_city | SQL column names / Python |
| Bracket ([]) | user > address > city | user[address][city] | PHP / form data |
| Slash (/) | user > address > city | user/address/city | File paths / REST APIs |
| Double underscore (__) | user > address > city | user__address__city | Django / environment vars |
Frequently Asked Questions
Code Examples
How to Flatten JSON in Python
Flattening JSON in Python can be done recursively by traversing nested objects and building dot-notation keys. Use a recursive function to walk through dictionaries and arrays, concatenating keys with a delimiter like . or _. Libraries like flatten-json or pandas json_normalize() provide ready-made solutions. Perfect for converting nested API responses into flat CSV files, preparing data for SQL databases, or creating spreadsheet-compatible formats.
import json
def flatten_json(obj, parent_key="", sep="."):
items = {}
for key, value in obj.items():
new_key = f"{parent_key}{sep}{key}" if parent_key else key
if isinstance(value, dict):
items.update(flatten_json(value, new_key, sep))
elif isinstance(value, list):
for i, item in enumerate(value):
arr_key = f"{new_key}[{i}]"
if isinstance(item, dict):
items.update(flatten_json(item, arr_key, sep))
else:
items[arr_key] = item
else:
items[new_key] = value
return items
# Usage
with open("nested.json") as f:
data = json.load(f)
flat = flatten_json(data)
print(json.dumps(flat, indent=2))How to Flatten JSON in JavaScript
Flattening JSON in JavaScript uses recursive functions to traverse nested objects and build flattened key-value pairs. Check object types with typeof and Array.isArray(), then concatenate keys with your chosen delimiter. Libraries like flat or flatten-object provide tested implementations. Ideal for Node.js backends, data transformation pipelines, or preparing JSON for CSV export in web applications.
function flattenJSON(obj, parentKey = "", sep = ".") {
const result = {};
for (const [key, value] of Object.entries(obj)) {
const newKey = parentKey ? `${parentKey}${sep}${key}` : key;
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
Object.assign(result, flattenJSON(value, newKey, sep));
} else if (Array.isArray(value)) {
value.forEach((item, index) => {
const arrKey = `${newKey}[${index}]`;
if (item !== null && typeof item === "object") {
Object.assign(result, flattenJSON(item, arrKey, sep));
} else {
result[arrKey] = item;
}
});
} else {
result[newKey] = value;
}
}
return result;
}
// Usage
const nested = { user: { name: "Alice", scores: [10, 20] } };
console.log(flattenJSON(nested));Complete Flattening Guide
When you actually need to flatten JSON
You need to flatten JSON when exporting to CSV or Excel because those formats require a flat structure with one value per cell. Nested objects don't fit into rows and columns without flattening first.
Loading into a SQL database or Google Sheets also requires flattening. Column names come from the dot-notation keys (user.address.city becomes a column header), and each nested value gets its own column.
Data analytics tools like Pandas don't handle nested dictionaries well. They expect flat DataFrames where each column is a simple value, not another dictionary. Flattening before loading into Pandas makes analysis much easier.
Most developers flatten JSON because their next tool doesn't speak nested JSON. If your destination is a spreadsheet, database, or analytics tool, you'll need to flatten.
Choosing your delimiter: dot, underscore, or custom
Dot notation (user.address.city) is the most common and readable delimiter. It's what most people expect when they see flattened JSON, and it works well for general use.
Underscore (user_address_city) is better when the output will become column headers in a database or spreadsheet. Dots in column names cause issues in SQL (they're interpreted as table.column syntax) and some BI tools don't handle them well.
Use a custom delimiter only if your key names already contain dots or underscores. For example, if you have keys like user.name or user_id, you'll need a different delimiter like a dash or double underscore to avoid confusion.
For most cases, stick with dots for readability or underscores for database compatibility. Custom delimiters are rarely needed unless you have naming conflicts.
Arrays inside nested JSON - what the flattener does
Arrays get indexed when flattened. If you have user.orders with two orders, the flattened output will have user.orders.0.id, user.orders.1.id, and so on. Each array element gets its own set of keys.
This creates one row per object if you export to CSV, which may not be what you want. If you have 10 orders in an array, you'll get 10 rows for that one user, each with a different order.
If you need one row per top-level object with arrays as a single value, use JSON.stringify on the array field before flattening. That way the entire array becomes a single string value in one cell instead of being split across multiple rows.
Related JSON Guides

How to Read JSON File in JavaScript: Fetch API, Node.js & ES6 (2026)
Read JSON files in JavaScript using Fetch API, Node.js fs module, and ES6 imports. Complete guide with code examples for browser and server environments, error handling, and large file processing.

How to Parse JSON in Python: json.loads() & json.load() Guide (2026)
Parse JSON in Python using json.loads() for strings and json.load() for files. Complete guide with code examples, error handling, nested data, and real-world use cases.

How Does JSON Work? Parsing, Serialization & Data Exchange (2026)
Learn how JSON works internally with serialization, parsing, and network communication. Complete technical guide covering JSON structure, syntax rules, performance, and cross-language compatibility.