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

后端 未结 30 2599
孤独总比滥情好
孤独总比滥情好 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:15
    $ echo '{ "foo": "lorem", "bar": "ipsum" }' \
    > | python -c'import fileinput, json;
    > print(json.dumps(json.loads("".join(fileinput.input())),
    >                  sort_keys=True, indent=4))'
    {
        "bar": "ipsum",
        "foo": "lorem"
    }
    

    NOTE: It is not the way to do it.

    The same in Perl:

    $ cat json.txt \
    > | perl -0007 -MJSON -nE'say to_json(from_json($_, {allow_nonref=>1}), 
    >                                     {pretty=>1})'
    {
       "bar" : "ipsum",
       "foo" : "lorem"
    }
    

    Note 2: If you run

    echo '{ "Düsseldorf": "lorem", "bar": "ipsum" }' \
    | python -c'import fileinput, json;
    print(json.dumps(json.loads("".join(fileinput.input())),
                     sort_keys=True, indent=4))'
    

    the nicely readable word becomes \u encoded

    {
        "D\u00fcsseldorf": "lorem", 
        "bar": "ipsum"
    }
    

    If the remainder of your pipeline will gracefully handle unicode and you'd like your JSON to also be human-friendly, simply use ensure_ascii=False

    echo '{ "Düsseldorf": "lorem", "bar": "ipsum" }' \
    | python -c'import fileinput, json;
    print json.dumps(json.loads("".join(fileinput.input())),
                     sort_keys=True, indent=4, ensure_ascii=False)'
    

    and you'll get:

    {
        "Düsseldorf": "lorem", 
        "bar": "ipsum"
    }
    
    0 讨论(0)
  • 2020-11-22 17:15

    Or, with Ruby:

    echo '{ "foo": "lorem", "bar": "ipsum" }' | ruby -r json -e 'jj JSON.parse gets'
    
    0 讨论(0)
  • 2020-11-22 17:17

    Pygmentize

    I combine Python's json.tool with pygmentize:

    echo '{"foo": "bar"}' | python -m json.tool | pygmentize -g
    

    There are some alternatives to pygmentize which are listed in my this answer.

    Here is a live demo:

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

    You can simply use standard tools like jq or json_pp.

    echo '{ "foo": "lorem", "bar": "ipsum" }' | json_pp

    or

    echo '{ "foo": "lorem", "bar": "ipsum" }' | jq

    will both prettify output like the following (jq even more colorful):

    {
      "foo": "lorem",
      "bar": "ipsum"
    }
    

    The huge advantage of jq is that it can do A LOT more if you'd like to parse and process the json.

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

    On *nix, reading from stdin and writing to stdout works better:

    #!/usr/bin/env python
    """
    Convert JSON data to human-readable form.
    
    (Reads from stdin and writes to stdout)
    """
    
    import sys
    try:
        import simplejson as json
    except:
        import json
    
    print json.dumps(json.loads(sys.stdin.read()), indent=4)
    sys.exit(0)
    

    Put this in a file (I named mine "prettyJSON" after AnC's answer) in your PATH and chmod +x it, and you're good to go.

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

    You can use this simple command to achieve the result:

    echo "{ \"foo\": \"lorem\", \"bar\": \"ipsum\" }"|python -m json.tool
    
    0 讨论(0)
提交回复
热议问题