Created
June 11, 2017 23:13
-
-
Save stigok/7f6e06bc1f3fd3e8584759f4e144834e to your computer and use it in GitHub Desktop.
Return value of property with specified key in JSON object read from stdin
This file contains 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 | |
const args = process.argv.slice(2) | |
const key = args[0] | |
if (!key) { | |
console.error('Missing argument') | |
process.exit(1) | |
} | |
let buffer = '' | |
process.stdin.on('data', (data) => { | |
if (data) buffer += data.toString() | |
}) | |
process.stdin.on('end', () => { | |
const obj = JSON.parse(buffer) | |
const result = obj[key] | |
if (typeof result === 'undefined') { | |
process.exit(2) | |
} | |
console.log(result) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example