How can I pretty-print a JSON file from the comman

2019-01-21 13:15发布

问题:

I've a file with a sequence of JSON element:

{ element0: "lorem", value0: "ipsum" }
{ element1: "lorem", value0: "ipsum" }
...
{ elementN: "lorem", value0: "ipsum" }

Is there a shell script to format JSON to display file content in a readable form?

I've seen this post, and I think is a good starting point!

My idea is to iterate rows in the file and then:

while read row; do echo ${row} | python -mjson.tool; done < "file_name"

Does anyone have any other ideas?

回答1:

Pipe the results from the file into the python json tool 2.6 onwards

cat 'file_name' | python -m json.tool


回答2:

You can use Python JSON tool (requires Python 2.6+).

For example:

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

Which will give you:

{
    "element0": "lorem",
    "element1": "ipsum"
}


回答3:

jq - a lightweight and flexible command-line JSON processor

I felt this deserved its own entry when it took me longer than it should have to discover. I was looking for a simple way to pretty-print the json output of docker inspect -f. It was mentioned briefly above by Noufal Ibrahim as part of another answer.

From the jq website (https://stedolan.github.io/jq/):

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

It provides colored output by default and you simply have to pipe to jq.

Example:

"Raw" json output vs the same piped to jq



回答4:

Colored output using Pygmentize + Python json.tool

Pygmentize is a killer tool. See this. I combine python json.tool with pygmentize

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

For other similar tools and installation instruction see the answer linked above.

Here is a live demo:



回答5:

There are a bunch of them. I personally have this alias in my .zshrc

pjson () {
        ~/bin/pjson.py | less -X
}

where pjson.py is

#!/usr/bin/env python

import json
import sys

try:
    input_str = sys.stdin.read()
    print json.dumps(json.loads(input_str), sort_keys = True, indent = 2)
except ValueError,e:
    print "Couldn't decode \n %s \n Error : %s"%(input_str, str(e))

Allows me to use that in a command line as a pipe (something like curl http://.... | pjson).

OTOH, Custom code is a liability so there's jq, which to me looks like the gold standard. It's written in C (and is hence portable with no dependencies like Python or Node), does much more than just pretty printing and is fast.



回答6:

Shawn's solution but for Python 3:

echo '{"foo": "bar"}' | python3 -m json.tool


回答7:

You can use jq package which can be installed in all Linux systems. Install the tool using below commands.

# Redhat based systems(Centos)
yum install -y jq

# Debian based systems
apt install -y jq

Then you will be able to pipe text streams to the jq tool.

echo '{"test":"value", "test2":"value2"}' | jq

Hope this answer will help.