Pretty Printing JSON in Shell Script
JSON is a textual method for representing JavaScript object literals and arrays and scalar data. It is relatively easier to read and write, and easier for manageable software to parse and generate.
JSON is commonly used to serialize structured data and exchange it over a network, usually between a server and a web application.
Pretty Printing JSON in Shell Script
When working with JSON, one might want to format its output in a more human-readable format. Consider the following JSON object:
{"name": "Ali", "rollno": 123, "fathername": "Muhammad", "mothername": "Qimra", "semester": 5, "roomno": "b-12", }
Understanding the properties of the above JSON object is difficult enough with a dozen properties, so imagine what a nightmare it would be if there were hundreds of properties.
Therefore, it is important to format it into a more human-friendly format, such as the following JSON property:
{"name": "Ali",
"rollno": 123,
"fathername": "Muhammad",
"mothername": "Qimra",
"semester": 5,
"roomno": "b-12"}
Notice how much cleaner it looks now. We will now go through a number of ways to achieve this.
With python 2.6+ you can use:
echo '{"key1": "val1", "keyn": "valn"}' | python -m json.tool
# or use
echo '{"key1": "val1", "keyn": "valn"}' | python2 -m json.tool
# or use
echo '{"key1": "val1", "keyn": "valn"}' | python3 -m json.tool
The output will look like this:
{
"key1": "val1",
"keyn": "valn"
}
However, if you do not have Python, Python2, or Python3 installed, you will receive one of the following errors:
Command 'python' not found
Command 'python2' not found
Command 'python3' not found
To avoid this error, use the following command:
sudo apt install python python2 python3
Another way is, if you store the JSON object in a JSON file, run the following command:
python -m json.tool <path to json file>.json
If, just in case, the JSON comes from an internet source like an API, then use:
curl <url> | python -m json.tool
We will now see other ways to achieve the above goals.
Suppose we have a file my_json.json with the following structure:
{ "key1": "val1", " keyn": "valn" }
Note that we use .
to specify the current directory, since the file resides in that directory:
sudo jq . my_json.json | less –R
jq '.key1' <<< '{ "key1": "val1", " keyn": "valn" }'
cat my_json.json| jq .
echo '{ "key1": "val1", " keyn": "valn" }' | npx json
To use jq, you must install it using the following command:
sudo apt install jq
The output of the first, third (using a pipe to transfer data from the file my_json.json to the process jq), and fourth methods are as follows:
{
"key1": "val1",
"keyn": "valn"
}
The fourth method requires npx
a dependency, but don't worry when running that command. If you don't have npx installed, it will be installed automatically.
The second method defines a JSON object and prints the value of key1. Here is its output:
"val1"
We can also combine Python's json.tool with pigmentize to pretty-print JSON objects. Consider the following command:
echo '{ "key1": "val1", " keyn": "valn" }' | python -m json.tool | pygmentize –g
The output of the above command is as follows:
{
"key1": "val1",
"keyn": "valn"
}
However, to use the command above, you must install pigmentize. To install pigmentize, use the following command:
sudo apt install python3-pygments
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Executing Shell Scripts in Node.js
Publish Date:2025/04/17 Views:140 Category:Node.js
-
In this short article, we will learn how to execute shell scripts using Node.js. Execute Shell Scripts in Node.js using the shelljs module ShellJS is a portable implementation of Unix shell commands on top of the Node.js API. We can use it
Run Shell Scripts and Open Shell Files in PHP
Publish Date:2025/04/12 Views:88 Category:PHP
-
PHP allows us to use shell_exec(); the shell function to process files. However, if your operating system is Windows, you should consider using the popen() and pclose() functions, because pipes are executed in text mode, which usually preve
Purpose of Shell Script Headers
Publish Date:2025/04/05 Views:63 Category:OPERATING SYSTEM
-
In programming, a shebang is a series of tags and exclamation marks #! at the beginning of a file. It then provides the entire path to the interpreter that will execute the file's code /bin/bash . After that, comes the code itself. The inte
Pause a Bash Shell Script for X seconds
Publish Date:2025/03/20 Views:90 Category:OPERATING SYSTEM
-
Bash scripts are useful for running multiple commands that you might not want to type line by line into a Bash command shell. In some cases, you might need to pause a script, perhaps to accept input from the user, or to sleep before returni
从 Powershell 脚本运行 BAT 文件
Publish Date:2024/03/15 Views:406 Category:编程语言
-
本教程说明了从 PowerShell 脚本运行 BAT 文件的不同方法。
以管理员身份运行 PowerShell 脚本
Publish Date:2024/03/01 Views:141 Category:编程语言
-
本文将展示如何在不输入管理员凭据的情况下以管理员权限运行 powershell 脚本。如果代码片段未在提升的 Windows PowerShell 中运行,则以下脚本将自行提升你的脚本。
获取 PowerShell 脚本的当前位置
Publish Date:2024/02/29 Views:139 Category:编程语言
-
本文将讨论如何在不同版本的 PowerShell 脚本环境中获取 PowerShell 脚本的当前位置。