Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created September 1, 2013 06:16
Show Gist options
  • Save shuhei/6402654 to your computer and use it in GitHub Desktop.
Save shuhei/6402654 to your computer and use it in GitHub Desktop.
Print prettified JSON from file or STDIN. Save this file as `~/bin/json`.
#!/usr/bin/env node
var filename = process.argv[2];
if (filename) {
var json = require('fs').readFileSync(filename).toString('utf-8');
printPretty(json);
} else {
var json = '';
process.stdin.setEncoding('utf-8');
process.stdin.on('data', function (chunk) {
json += chunk;
});
process.stdin.on('end', function () {
printPretty(json);
});
}
function printPretty(json) {
var pretty = JSON.stringify(JSON.parse(json), null, 2);
console.log(pretty);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment