Last active
July 19, 2016 20:17
-
-
Save ddnn55/88c757bc548d9cf46cf37dfe8cec910c to your computer and use it in GitHub Desktop.
List all paths in the JSON object on stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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