Skip to content

Instantly share code, notes, and snippets.

@ddnn55
Last active July 19, 2016 20:17
Show Gist options
  • Save ddnn55/88c757bc548d9cf46cf37dfe8cec910c to your computer and use it in GitHub Desktop.
Save ddnn55/88c757bc548d9cf46cf37dfe8cec910c to your computer and use it in GitHub Desktop.
List all paths in the JSON object on stdin
#!/usr/bin/env node
var readline = require('readline');
var devnull = require('dev-null');
var rl = readline.createInterface({
input: process.stdin,
output: devnull()
});
var jsonString = '';
rl.on('line', function(line) {
jsonString = jsonString + line;
});
rl.on('close', function() {
var har = JSON.parse(jsonString);
console.log(object2keys(har, '').join('\n'));
});
function object2keys(obj, path)
{
var keys = [];
for (var key in obj)
{
if (obj.hasOwnProperty(key))
{
if ('object' == typeof(obj[key]))
{
keys = keys.concat(
object2keys(obj[key],
path +
(Array.isArray(obj) ?
'[]' :
(path ? '.' : '') + key
)
)
);
}
else
{
keys.push(path + '.' + key);
}
}
}
return Array.from(new Set(keys));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment