JSON Flattener
Flatten nested JSON into flat key-value pairs with dot notation.
About this tool
JSON flattening is the process of converting a nested JSON structure into a flat, single-level representation using dot notation keys. When you're working with complex nested objects from APIs, databases, or configuration files, this tool transforms them into simple key-value pairs that are easier to analyze, store, or export to formats like CSV or spreadsheets.
To use the JSON Flattener, paste your nested JSON into the input area and click the flatten button. The tool instantly converts multilevel objects and arrays into flat keys where parent-child relationships are represented by dots—for example, "user.profile.email" instead of nested braces. You can then copy the result directly or export it for use in data pipelines, reporting tools, or spreadsheet applications.
This tool is invaluable for data engineers transforming unstructured API responses, developers debugging complex object structures, and analysts preparing data for BI platforms that expect flattened schemas. The JSON Flattener respects array indices and custom separators, making it flexible for various data transformation workflows.
Frequently Asked Questions
Code Implementation
def flatten(obj, prefix="", sep="."):
"""Flatten a nested dict into dot-notation keys."""
result = {}
if isinstance(obj, dict):
for key, value in obj.items():
new_key = f"{prefix}{sep}{key}" if prefix else key
if isinstance(value, (dict, list)):
result.update(flatten(value, new_key, sep))
else:
result[new_key] = value
elif isinstance(obj, list):
for i, value in enumerate(obj):
new_key = f"{prefix}{sep}{i}" if prefix else str(i)
if isinstance(value, (dict, list)):
result.update(flatten(value, new_key, sep))
else:
result[new_key] = value
else:
result[prefix] = obj
return result
def unflatten(flat, sep="."):
"""Reconstruct nested dict from dot-notation keys."""
result = {}
for key, value in flat.items():
parts = key.split(sep)
d = result
for part in parts[:-1]:
d = d.setdefault(part, {})
d[parts[-1]] = value
return result
# Example
nested = {"user": {"name": "Alice", "address": {"city": "Seoul"}}}
flat = flatten(nested)
print(flat) # {'user.name': 'Alice', 'user.address.city': 'Seoul'}
print(unflatten(flat)) # back to nestedComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.