Skip to content

Instantly share code, notes, and snippets.

@mesprague
Forked from kristopherjohnson/formatjson.js
Created December 15, 2016 08:44
Show Gist options
  • Save mesprague/6ec9aab348baa911e0e041055c5f3ab9 to your computer and use it in GitHub Desktop.
Save mesprague/6ec9aab348baa911e0e041055c5f3ab9 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