Skip to content

Instantly share code, notes, and snippets.

@ryankirkman
Forked from kristopherjohnson/formatjson.js
Last active September 16, 2019 02:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryankirkman/76a95bb3ac2bb1a4e326 to your computer and use it in GitHub Desktop.
Save ryankirkman/76a95bb3ac2bb1a4e326 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 input = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
input += chunk;
});
process.stdin.on('end', function () {
var parsedData = JSON.parse(input);
var outputJSON = JSON.stringify(parsedData, null, ' ');
process.stdout.write(outputJSON);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment