How can I pretty-print JSON in a shell script?

后端 未结 30 2598
孤独总比滥情好
孤独总比滥情好 2020-11-22 16:27

Is there a (Unix) shell script to format JSON in human-readable form?

Basically, I want it to transform the following:

{ \"foo\": \"lorem\", \"bar\":         


        
相关标签:
30条回答
  • 2020-11-22 17:19

    jj is super-fast, can handle ginormous JSON documents economically, does not mess with valid JSON numbers, and is easy to use, e.g.

    jj -p # for reading from STDIN
    

    or

    jj -p -i input.json
    

    It is (2018) still quite new so maybe it won’t handle invalid JSON the way you expect, but it is easy to install on major platforms.

    0 讨论(0)
  • 2020-11-22 17:20

    Check out Jazor. It's a simple command line JSON parser written in Ruby.

    gem install jazor
    jazor --help
    
    0 讨论(0)
  • 2020-11-22 17:21

    With Python 2.6+ you can do:

    echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
    

    or, if the JSON is in a file, you can do:

    python -m json.tool my_json.json
    

    if the JSON is from an internet source such as an API, you can use

    curl http://my_url/ | python -m json.tool
    

    For convenience in all of these cases you can make an alias:

    alias prettyjson='python -m json.tool'
    

    For even more convenience with a bit more typing to get it ready:

    prettyjson_s() {
        echo "$1" | python -m json.tool
    }
    
    prettyjson_f() {
        python -m json.tool "$1"
    }
    
    prettyjson_w() {
        curl "$1" | python -m json.tool
    }
    

    for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.

    Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys flag to the end. I.e. ... | python -m json.tool --save-keys.

    0 讨论(0)
  • 2020-11-22 17:21

    With Perl, use the CPAN module JSON::XS. It installs a command line tool json_xs.

    Validate:

    json_xs -t null < myfile.json
    

    Prettify the JSON file src.json to pretty.json:

    < src.json json_xs > pretty.json
    

    If you don't have json_xs, try json_pp . "pp" is for "pure perl" – the tool is implemented in Perl only, without a binding to an external C library (which is what XS stands for, Perl's "Extension System").

    0 讨论(0)
  • 2020-11-22 17:22

    UPDATE I'm using jq now as suggested in another answer. It's extremely powerful at filtering JSON, but, at its most basic, also an awesome way to pretty print JSON for viewing.

    jsonpp is a very nice command line JSON pretty printer.

    From the README:

    Pretty print web service responses like so:

    curl -s -L http://<!---->t.co/tYTq5Pu | jsonpp
    

    and make beautiful the files running around on your disk:

    jsonpp data/long_malformed.json
    

    If you're on Mac OS X, you can brew install jsonpp. If not, you can simply copy the binary to somewhere in your $PATH.

    0 讨论(0)
  • 2020-11-22 17:23

    I usually just do:

    echo '{"test":1,"test2":2}' | python -mjson.tool
    

    And to retrieve select data (in this case, "test"'s value):

    echo '{"test":1,"test2":2}' | python -c 'import sys,json;data=json.loads(sys.stdin.read()); print data["test"]'
    

    If the JSON data is in a file:

    python -mjson.tool filename.json
    

    If you want to do it all in one go with curl on the command line using an authentication token:

    curl -X GET -H "Authorization: Token wef4fwef54te4t5teerdfgghrtgdg53" http://testsite/api/ | python -mjson.tool
    
    0 讨论(0)
提交回复
热议问题