Skip to content

Instantly share code, notes, and snippets.

@rgamez
Last active October 19, 2015 18:32
Show Gist options
  • Save rgamez/38af3cf5d2bd1c4aad05 to your computer and use it in GitHub Desktop.
Save rgamez/38af3cf5d2bd1c4aad05 to your computer and use it in GitHub Desktop.
Pretty print a json skipping the BOM if present
#!/usr/bin/env node
var fs = require('fs');
process.stdout.on('error', function(err) {
if (err.code == "EPIPE") {
process.exit(0);
}
});
var prettyPrint = function(json) {
return JSON.stringify(JSON.parse(json), null, ' ');
};
fs.readFile('locations.json', function(err, buffer) {
if (err) {
console.error(err);
return;
}
var prettyJSON;
var bom = new Buffer(3);
buffer.copy(bom, 0);
if (bom.equals(new Buffer([0xef, 0xbb, 0xbf]))) {
prettyJSON = prettyPrint(buffer.slice(3));
}
else {
prettyJSON = prettyPrint(buffer);
}
process.stdout.write(prettyJSON);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment