hwu5uq
Last Updated: October 07, 2016
·
2.776K
· oleiade
Img 1416

Zsh function to pretty print json in shell

I found Luis Nell trick to easily pretty print json in shell or code editor very usefull, so I decided to port it into my zsh config as a function.

As it might be usefull to someone else, here's the code. Might be compatible with bash and other shells with a few adjustements.

function pjson {
    if [ $# -gt 0 ];
        then
        for arg in $@
        do
            if [ -f $arg ];
                then
                less $arg | python -m json.tool
            else
                echo "$arg" | python -m json.tool
            fi
        done
    fi
}

It can whether print from a string cmdline arg or a file, and arguments can be mixed.

pjson '{"test": "test"}'
or
pjson myjsonfile.json
or even
pjson '{"test": "test"}' myjsonfile.json will work.

Say Thanks
Respond

2 Responses
Add your response

3734
781bbe3e6880c14f783d044f2ff05e50

Shameless self promotion ahead

I hacked a simple nodejs module for doing precisely that https://github.com/tralamazza/prettyson

over 1 year ago ·
3794
3

Nifty one. Definitely love it.

over 1 year ago ·