Last active
December 22, 2015 08:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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\ - |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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