Easy CLI JSON Formatting(binarysludge.com)
binarysludge.com
Easy CLI JSON Formatting
http://www.binarysludge.com/2012/11/09/easy-cli-json-formatting/
9 comments
Couple dozen other ways to do it: http://stackoverflow.com/questions/352098/how-to-pretty-prin...
A problem with piping curl output through "python -mjson.tool" is that you can't include the response headers, which is sometimes useful.
(This was enough of a problem for me that I wrote a simple curl replacement in Python so I could have pretty-printed JSON or XML and response headers, or send the response directly to a Python REPL console as an object: https://github.com/cobralibre/hotpotato )
(This was enough of a problem for me that I wrote a simple curl replacement in Python so I could have pretty-printed JSON or XML and response headers, or send the response directly to a Python REPL console as an object: https://github.com/cobralibre/hotpotato )
Emit the headers to stderr?
curl -v -s "$url" | python -mjson.tool
curl -D /dev/stderr -s "$url" | python -mjson.toolHere is Paul Irish's .function dotfile solution: https://github.com/paulirish/dotfiles/blob/master/.functions....
Really recommend setting up his dotfiles. After you back up your dotfiles, you can install his (which overwrites yours) just by writing
Really recommend setting up his dotfiles. After you back up your dotfiles, you can install his (which overwrites yours) just by writing
git clone https://github.com/paulirish/dotfiles.git && cd dotfiles && ./sync.shThis is another good one for formatting / filtering json: http://stedolan.github.com/jq/
Here is the bash alias I have been using for CLI JSON formatting, and as a bonus, my random password generator:
alias jsonpp='ruby -r json -e '\''puts JSON.pretty_generate(JSON.parse(STDIN.read))'\'''
alias md5pass='dd if=/dev/random bs=100 count=1 2>/dev/null | md5'
alias jsonpp='ruby -r json -e '\''puts JSON.pretty_generate(JSON.parse(STDIN.read))'\'''
alias md5pass='dd if=/dev/random bs=100 count=1 2>/dev/null | md5'
I've done something similar a while back. A simple "works-for-me" package. Feel free to try it out:
https://github.com/exhuma/braindump/tree/master/jsonformat
https://github.com/exhuma/braindump/tree/master/jsonformat
httpie is a nice replacement for curl which, among other things, has built-in formatting so you can get pretty JSON / XML responses without giving up your ability to do things like displaying response headers:
https://github.com/jkbr/httpie
https://github.com/jkbr/httpie
I use jazor and coderay for that:
gem install jazor coderay
curl $SOMETHING | jazor | coderay -json
gem install jazor coderay
curl $SOMETHING | jazor | coderay -json
Or with jsonlist in NodeJS.