Hacker News new | past | comments | ask | show | jobs | submit login
PJSON: pretty, colorful json output formatting for terminal (igorgue.com)
105 points by auston on Sept 18, 2012 | hide | past | favorite | 34 comments



If this is based on pygments, is it reasonable to have a command like this that works on everything pygments knows how to highlight, and plugs into a bunch of pretty-printers / reformatters, e.g. GNU indent?

Is there an equivalent of pygments for pretty-printing all the things?


Yes, it's the "pygmentize" command, included in pygments. Also, pygments is a fantastic tool, I love it.



Here's a similar trick with the Python json module:

  echo '{"hello":"there"}' | python -mjson.tool


dang and all this time I've been doing this all this time:

   echo '{"hello": "there"}' | python -c "import json, sys; print json.dumps(json.load(sys.stdin), indent=4)"
Thanks for the tip!


And Perl's JSON::XS

     echo '{"hello":"there"}' | json_xs


Or for pretty colors, try Data::Printer

    echo '{"hello":"there"}' | perl -MJSON -MDDP -E '$f = decode_json readline; p $f'


Is there a colored JSON dumper in Perl?


yeah that's mentioned on the second line of TFA


Wait wait ... what are the unicode characters for that cloud and lightning bolt? That looks awesome.


Author here, unfortunately no thunder on Mountain Lion :( [1]

It's from my zsh theme: https://raw.github.com/igorgue/oh-my-zsh/master/themes/igorg... I stole it from the cloud theme and another theme that I don't remember.

The thunder lights up when the program doesn't end property (return 0).

[1] <3 thunder http://wiki.xkcd.com/irc/Heart_Thunder


> The thunder lights up when the program doesn't end property (return 0).

Inspired! Did you come up with that part?


Haha yes.

This is the code add it at the end of your prompt:

    function thunder() {
        echo "%(?,%{$fg_bold[grey]%}⚡%{$reset_color%},%{$fg_bold[yellow]%}⚡%{$reset_color%})"
    }


Thanks to gucharmap's excellent Unicode character search:

☁ U+2601 CLOUD

⚡ U+26A1 HIGH VOLTAGE SIGN


There is unicode characters (code points, graphemes, whatevs) for everything

http://unicodesnowmanforyou.com/



Why all of that complicated setup? Why not just support pip install pjson? :)


agreed, needs to be submitted to pypi now!


We recently discussed underscore-cli here on Hacker News (http://news.ycombinator.com/item?id=4491058) and I find it to offer more than this, but still a nice tool.

https://github.com/ddopson/underscore-cli


This looks really good, thanks.

Rather than pipe curl's output through stuff though, for the most part I use httpie [1]. It's a human-friendly curl wrapper.

[1] https://github.com/jkbr/httpie


Hey kids, why shouldn't every python tool have a Ruby counterpart? :) https://github.com/marksands/pjson-rb


Come on, you should use coderay instead of pygments. 100% ruby ;)


Sweet! I went with a quick conversion rather than doing any research. Thanks for the link, I'll definitely give it a whirl tomorrow evening.


This is awesome :)


I've been using json_reformat which comes from "yajl" to do this sort of thing for a while.

EDIT: I love seeing something that does an "awesome-print" with terminal colorization, though.


alias pjson='pygmentize -l json'


I'm curious how the author managed to learn the pygments library without learning it already provided a command. This script can be replaced by

alias pjson='pygmentize -l json'

If you're passing it the file name, pygmentize will figure out the lexer to use, e.g.

pygmentize foo.json

pygmentize bar.rb

It's only when you have it read from stdin that you need to specify, e.g.

pygmentize -l json < foo.json


"Small retina display images are fucking huge."

Even when reshown on retina displays, since the HTML standard is to attend to the pixel size and not the physical resolution. Screenshots taken with the standard Cmd-Shift-4 or similar all include proper resolution metadata, should someone want to show it at the correct size.


Not sure what this offers over what I currently have in ~bin with a similar name,

python -m json.tool "$@" | pygmentize -l json | less -RSNFX


For those that want to stay in Ruby land:

$ gem install jazor coderay

$ echo '{"foo": "bar"}' | jazor | coderay -json


oh man.. I was looking for exactly this earlier today.. it looks REALLY pretty



This here is my favorite. http://stackoverflow.com/a/6066102/3461


#cool




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: