Skip to content

Instantly share code, notes, and snippets.

@xmas
Forked from kristopherjohnson/formatjson.js
Created June 26, 2017 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmas/d06f89389bd53d463b6af9d98c0073ce to your computer and use it in GitHub Desktop.
Save xmas/d06f89389bd53d463b6af9d98c0073ce to your computer and use it in GitHub Desktop.
Read JSON from standard input and writes formatted JSON to standard output. Requires Node.js.
#!/usr/bin/env node
// Reads JSON from stdin and writes equivalent
// nicely-formatted JSON to stdout.
var stdin = process.stdin,
stdout = process.stdout,
inputChunks = [];
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
inputChunks.push(chunk);
});
stdin.on('end', function () {
var inputJSON = inputChunks.join(),
parsedData = JSON.parse(inputJSON),
outputJSON = JSON.stringify(parsedData, null, ' ');
stdout.write(outputJSON);
stdout.write('\n');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment