Skip to content

Instantly share code, notes, and snippets.

@alhafoudh
Created July 11, 2017 20:07
Show Gist options
  • Save alhafoudh/eafbfea9189385e243cd5461689b3b23 to your computer and use it in GitHub Desktop.
Save alhafoudh/eafbfea9189385e243cd5461689b3b23 to your computer and use it in GitHub Desktop.
const _ = require('lodash');
function printKeys(key, obj, parentKeyPath = '') {
const keyPath = `${parentKeyPath}.${key}`;
console.log(keyPath);
if (_.isArray(obj) && !_.isEmpty(obj)) {
printKeys(key, _.head(obj), keyPath);
} else if (_.isObject(obj)) {
const firstKey = _.head(_.keys(obj));
if (_.toNumber(firstKey).toString() === firstKey.toString()) {
const firstObj = obj[firstKey];
printKeys(firstKey, firstObj, keyPath);
} else {
_.map(obj, (newObj, newKey) => printKeys(newKey, newObj, keyPath));
}
}
}
let content = '';
process.stdin.resume();
process.stdin.on('data', (buf) => {
content += buf.toString();
});
process.stdin.on('end', () => {
const json = JSON.parse(content);
printKeys('ROOT', json, '', 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment