Skip to content

Instantly share code, notes, and snippets.

@oliveiraev
Created November 26, 2014 15:59
Show Gist options
  • Save oliveiraev/a900f84c0158fc985dbb to your computer and use it in GitHub Desktop.
Save oliveiraev/a900f84c0158fc985dbb to your computer and use it in GitHub Desktop.
Given an input (filename or stdin), prints out it space-indented
#!/usr/bin/env node
/**
* @name JSON Prettifier
* @summary Given an input (filename or stdin), prints out it space-indented
* @license [LGPLv3]{@link https://www.gnu.org/licenses/lgpl-3.0.txt}
* @version 0.0.1a
*
*/
(function () {
"use strict";
var buffer, output;
/**
* Stores formatted JSON into <code>shared</code> output var
* @param {String} input Unformatted json
*/
function prettify(input) {
input = JSON.parse(input);
output = JSON.stringify(input, null, 4);
}
/**
* Send data stored into <code>shared</code> to standard output
*/
function print() {
process.stdout.write(output + "\r\n");
}
buffer = process.stdin;
if (process.argv.length > 2) {
buffer = require("fs").createReadStream(process.argv[2]);
}
buffer.on("data", prettify);
buffer.on("end", print);
}());
@oliveiraev
Copy link
Author

node pretty_json /path/to/file.json
cat /path/to/file.json | node pretty_json

@guiBautista
Copy link

fails with {"content":{"rendered":"<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ultricies nunc id tellus cursus volutpat. Vivamus \u0003ac rhoncus quam"}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment