Skip to content

Instantly share code, notes, and snippets.

@bruno-brant
Created October 20, 2019 20:15
Show Gist options
  • Save bruno-brant/27088b018a22a6deedd4f6945a028a08 to your computer and use it in GitHub Desktop.
Save bruno-brant/27088b018a22a6deedd4f6945a028a08 to your computer and use it in GitHub Desktop.
Get field from json
// Small tool to obtain a field from a JSON file
// Read the JSON from STDIN
var buff = "";
if (process.argv.length <= 1) {
console.error("Must inform the field to be extracted");
process.exit(-1);
}
const field = process.argv[2];
process
.stdin
.on('data', data => { buff
buff += data;
})
.on('end', () => {
const json = JSON.parse(buff);
if (json[field] === undefined) {
console.error("ERROR: field is undefined");
process.exit(-1);
}
console.log(json[field]);
});
@bruno-brant
Copy link
Author

Very simple utility, lacking a lot of error handling and such, just to parse a JSON from STDIN and output a certain field

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment