Created
October 20, 2019 20:15
-
-
Save bruno-brant/27088b018a22a6deedd4f6945a028a08 to your computer and use it in GitHub Desktop.
Get field from json
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
// 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]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very simple utility, lacking a lot of error handling and such, just to parse a JSON from STDIN and output a certain field