- Download jsonPrint.js
- Make it executable (
chmod +x ~/Download/jsonPrint.js
) - Move it into your bin folder (
mv jsonPrint.js /usr/local/bin/
)
curl http://some-random-json-api.com/json/data | jsonPrint
#!/usr/bin/env node | |
var data = '' | |
process.stdin.resume() | |
process.stdin.setEncoding('utf8') | |
process.stdin.on('data', function(chunk) { | |
data += chunk; | |
}) | |
process.stdin.on('end', function() { | |
try { | |
const jsonObj = JSON.parse(data) | |
console.log(`--- <JSON print> ---`) | |
console.log(JSON.stringify(jsonObj, null, 2)) | |
console.log(`--- <JSON print/> ---`) | |
} catch (e) { | |
console.log(`--- JSON print: No valid JSON found, plain output: ---`) | |
console.log(data) | |
} | |
process.exit(0) | |
}) |