json-flatten Flatten nested JSON to dot-notation paths
Flags
--separator string default: =
Examples
Flatten nested JSON to dot-notation
Usage
"{"user":{"name":"Alice","address":{"city":"Paris"}},"tags":[..." | json-flatten Flatten a nested config file to see all settings at a glance
Usage
"{"server":{"host":"prod.example.com","port":443,"ssl":{"enab..." | json-flatten Flatten a nested API response for pasting into a spreadsheet
Usage
"{"order":{"id":"12345","customer":{"name":"Bob","email":"bob..." | json-flatten View source
(input, opts = {})=>{
try {
const sep = opts.separator ?? " = ";
const result = [];
const flatten = (obj, prefix)=>{
for (const [k, v] of Object.entries(obj)){
const path = prefix ? `${prefix}.${k}` : k;
if (v && typeof v === "object") flatten(v, path);
else result.push(`${path}${sep}${v}`);
}
};
flatten(JSON.parse(input), "");
return result.join("\n");
} catch (e) {
return `Error: ${e.message}`;
}
}