in Technology by
How can we pretty-print JSON in a shell script?

1 Answer

0 votes
by

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 --sort-keys.

Related questions

0 votes
    Jupyter notebooks (Python) return the value of the last variable in a cell in a pretty printed format. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    In shell script, How can I set 0 as default value to parameter. param2=0 ${2:-0} and ${2 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    In shell script, How can I set 0 as default value to parameter. param2=0 ${2:-0} and ${2 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    In shell script, How can I set 0 as default value to parameter. param2=0 ${2:-0} and ${2 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    In Intellij IDE's such as PyCharm or Idea there is an option to customize the menu and toolbar by ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have a parameters.ini file, such as: [parameters.ini] database_user = user database_version = 20110611142248 I want to ... I do this? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    What is the use of "$?" sign in shell script?...
asked May 28, 2021 in Technology by JackTerrance
0 votes
    What are the different types of variables used in shell script?...
asked May 28, 2021 in Technology by JackTerrance
0 votes
    Why is the use of shell script?...
asked May 28, 2021 in Technology by JackTerrance
0 votes
    What is shell script?...
asked May 28, 2021 in Technology by JackTerrance
0 votes
    What is the use of "$?" sign in shell script?...
asked Jan 19, 2021 in Technology by JackTerrance
0 votes
    What are the different types of variables used in shell script?...
asked Jan 19, 2021 in Technology by JackTerrance
0 votes
    What is the use of shell script?...
asked Jan 19, 2021 in Technology by JackTerrance
0 votes
    I want to run a python script from a shell script. I have been able to run it from the "command ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    I want to run a python script from a shell script. I have been able to run it from the "command ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
...