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

后端 未结 30 3032
孤独总比滥情好
孤独总比滥情好 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:33

    I wrote a tool that has one of the best "smart whitespace" formatters available. It produces more readable and less verbose output than most of the other options here.

    underscore-cli

    This is what "smart whitespace" looks like:

    I may be a bit biased, but it's an awesome tool for printing and manipulating JSON data from the command-line. It's super-friendly to use and has extensive command-line help/documentation. It's a Swiss Army knife that I use for 1001 different small tasks that would be surprisingly annoying to do any other way.

    Latest use-case: Chrome, Dev console, Network tab, export all as HAR file, "cat site.har | underscore select '.url' --outfmt text | grep mydomain"; now I have a chronologically ordered list of all URL fetches made during the loading of my company's site.

    Pretty printing is easy:

    underscore -i data.json print
    

    Same thing:

    cat data.json | underscore print
    

    Same thing, more explicit:

    cat data.json | underscore print --outfmt pretty
    

    This tool is my current passion project, so if you have any feature requests, there is a good chance I'll address them.

提交回复
热议问题