Skip to content

Instantly share code, notes, and snippets.

@kaeff
Last active December 22, 2015 08:48
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 kaeff/6447276 to your computer and use it in GitHub Desktop.
Save kaeff/6447276 to your computer and use it in GitHub Desktop.
JSON prettyfying, requires NodeJS. I use from VIM in combination with the vim-json plugin for syntax highlighting and extra sweetness. See https://github.com/elzr/vim-json
" Settings for vim-json
au! BufRead,BufNewFile *.json set filetype=json
au FileType json setlocal foldmethod=syntax
" Use jsonpp for auto-indendting
au FileType json setlocal equalprg=jsonpp\ -
#! /usr/local/bin/node
// vim: set filetype=javascript
var fs = require('fs');
var sys = require('sys');
var main = function () {
var fileName;
if (process.argv[2]) {
fileName = process.argv[2].toString();
}
if (fileName === undefined) {
sys.puts("USAGE: "+process.argv[1].split('/').reverse()[0]+" <FileName>");
} else if (fileName === "-") {
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
process.stdout.write(prettyJson(data));
});
} else {
var input = fs.readFileSync(fileName).toString();
sys.puts(prettyJson(input));
}
}
var prettyJson = function(input) {
var json = JSON.parse(input);
return JSON.stringify(json, null, 2);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment