Created
September 1, 2013 06:16
-
-
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`.
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 | |
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