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

后端 未结 30 3105
孤独总比滥情好
孤独总比滥情好 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: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").

提交回复
热议问题