Skip to content

Instantly share code, notes, and snippets.

@aGuyNamedJonas
Last active January 29, 2018 10:22
Show Gist options
  • Save aGuyNamedJonas/cc04c15afb22847e6553a0cf4ca847af to your computer and use it in GitHub Desktop.
Save aGuyNamedJonas/cc04c15afb22847e6553a0cf4ca847af to your computer and use it in GitHub Desktop.
Pretty print piped JSON
#!/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)
})

Install

  • Download jsonPrint.js
  • Make it executable (chmod +x ~/Download/jsonPrint.js)
  • Move it into your bin folder (mv jsonPrint.js /usr/local/bin/)

Use

curl http://some-random-json-api.com/json/data | jsonPrint

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