JSON Flattener
Convert nested JSON into flat key-value paths
JSON Flattener — what it does
Convert nested JSON into a flat set of key-value pairs, where each key is the full dot path to its value and the separator is yours to choose. Flattening is what makes deep JSON fit into things that have no concept of nesting: spreadsheets, CSV files, environment variables and log fields.
Worked example
One order record with a nested customer object, two line items and a null note, flattened with the dot separator.
Input — nested JSON
{"order_id":1004,
"customer":{"name":"Dev Patel","email":"dev.patel@example.com"},
"items":[{"sku":"PB-400","qty":20},{"sku":"NB-100","qty":2}],
"shipped":false,
"notes":null}Output — flat key-value pairs
{
"order_id": 1004,
"customer.name": "Dev Patel",
"customer.email": "dev.patel@example.com",
"items.0.sku": "PB-400",
"items.0.qty": 20,
"items.1.sku": "NB-100",
"items.1.qty": 2,
"shipped": false,
"notes": null
}Array positions become numeric segments (items.0.sku); false and null keep their JSON types. With the underscore separator the same keys read customer_name, items_0_sku and so on.
When this tool helps
- Preparing a nested API response for import into a spreadsheet or CSV.
- Turning a nested configuration file into flat environment-variable-style keys.
- Getting a readable overview of an unfamiliar payload's full structure at a glance.
How to use it
- Paste the JSON document.
- Set the key separator, typically a dot.
- Flatten the JSON.
- Copy the flattened key-value pairs.
Accuracy, limits, and good practice
- Array elements become indexed keys such as items.0.name, which is exactly what you need for a CSV column header.
- Choose a separator that does not occur in your key names, or the flattened paths become ambiguous to anything reading them back.
- Flattening is a one-way trip in practice — rebuilding the nesting requires knowing which segments were arrays and which were objects.
Frequently asked questions
Why flatten at all?
Because tables and CSV have no nesting. Flat paths are the standard way to represent hierarchy in a column header.
What happens to empty objects and arrays?
They have no leaf values, so they contribute no rows; only paths that end in a scalar value appear.
Can I go the other way?
Not with this tool. Reconstructing nesting from paths needs a parser that knows the intended shape.
What if a key already contains the separator?
Nothing distinguishes it: {"a.b":1} and {"a":{"b":1}} both flatten to the key a.b. Switch to the underscore separator when keys contain dots, or the other way round.
How is a top-level array flattened?
The index comes first: an array of orders yields 0.order_id, 1.order_id and so on. Values are never altered (null, false and numbers keep their types), and the result is only shown and copied, not downloaded.
Related tools on this site
- CSV Column Extractor — Keep only selected columns and download a new CSV
- CSV File Difference — Compare two CSV files by a key column and export differences
- CSV Sorter — Sort CSV rows by a text, number, or date column
Further reading: Cleaning a CSV export before you import it anywhere · CSV, JSON, YAML, XML and Markdown Tables: When Each Fits and How to Convert
Everything above runs inside this page. JSON Flattener needs no account, no upload, and no server round trip — close the tab and nothing is left behind.
JSON 扁平化能做什麼
把巢狀 JSON 轉成扁平的鍵值組,每個鍵都是通往該值的完整點路徑,分隔符由你決定。攤平能讓深層 JSON 塞進沒有巢狀概念的東西裡:試算表、CSV、環境變數與日誌欄位。
實際範例
一筆訂單紀錄,含巢狀 customer 物件、兩個品項與值為 null 的備註,以點號分隔符攤平。
來源:巢狀 JSON
{"order_id":1004,
"customer":{"name":"Dev Patel","email":"dev.patel@example.com"},
"items":[{"sku":"PB-400","qty":20},{"sku":"NB-100","qty":2}],
"shipped":false,
"notes":null}攤平後的鍵值對
{
"order_id": 1004,
"customer.name": "Dev Patel",
"customer.email": "dev.patel@example.com",
"items.0.sku": "PB-400",
"items.0.qty": 20,
"items.1.sku": "NB-100",
"items.1.qty": 2,
"shipped": false,
"notes": null
}陣列位置變成數字片段(items.0.sku);false 與 null 保留 JSON 型別。改成底線分隔符時,同樣的鍵會寫成 customer_name、items_0_sku 等。
什麼時候用得上
- 把巢狀 API 回應整理成可匯入試算表或 CSV 的形式。
- 把巢狀設定檔轉成環境變數風格的扁平鍵名。
- 一眼掌握陌生載荷的完整結構。
使用步驟
- 貼上 JSON 文件。
- 設定鍵名分隔符,通常用點。
- 執行攤平。
- 複製攤平後的鍵值組。
精確度、限制與實務建議
- 陣列元素會變成帶索引的鍵,例如 items.0.name,正好可以當 CSV 的欄位標題。
- 分隔符請挑不會出現在鍵名中的字元,否則攤平後的路徑對讀取端會產生歧義。
- 實務上攤平是單向的——要還原巢狀,必須知道哪些層級原本是陣列、哪些是物件。
常見問題
為什麼要攤平?
因為表格與 CSV 沒有巢狀。用扁平路徑當欄位標題,是表達階層的標準做法。
空物件與空陣列會怎樣?
它們沒有葉節點值,因此不會產生任何項目;只有以純量值結尾的路徑會出現。
可以反向還原嗎?
本工具不行。從路徑重建巢狀,需要知道原本預期結構的解析器。
鍵本身就含分隔符怎麼辦?
沒有任何東西能區分:{"a.b":1} 與 {"a":{"b":1}} 攤平後都是鍵 a.b。鍵裡有點號就改成底線分隔符,反之亦然。
最外層是陣列時怎麼攤平?
索引放最前面:訂單陣列會得到 0.order_id、1.order_id 依此類推。值完全不動(null、false 與數字保留型別),而且產生的內容只能顯示與複製,沒有下載。
本站相關工具
- CSV 欄位提取 — 保留選取欄位並下載新的 CSV
- CSV 檔案差異比較 — 依鍵值欄位比較兩個 CSV 並匯出差異
- CSV 排序工具 — 依文字、數字或日期欄位排序 CSV 資料
延伸閱讀: 匯入之前先把 CSV 匯出檔清乾淨 · CSV、JSON、YAML、XML 與 Markdown 表格:各自適用的場合與轉換方法
以上步驟全部在這個頁面內完成,JSON 扁平化 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。