如何在 Linux 终端上漂亮地打印 JSON 文件
JSON
![](/data/attachment/album/202012/13/122331h00a3hc63ttoc630.jpg)


[JSON](https://www.json.org) 文件非常棒，因为它们以人类可读的格式存储数据集合。然而，如果 JSON 文件被最小化过，那么阅读 JSON 文件可能会很痛苦。


以这个为例：


![Minified JSON is difficult to read](/data/attachment/album/202012/13/122430wqgtp1gjtot17gs0.png)


计算机可以很容易地读取它。即使是人也能读，但如果 JSON 文件以合适的格式显示，那么阅读就会简单很多。我的意思是 JSON 文件应该是这样读的：


![Pretty Printed JSON is easier to read](/data/attachment/album/202012/13/122430nliz1tiujd15jklk.png)


你可以使用大多数的文本编辑器和一些插件以合适的格式显示它。然而，如果你在终端中，或者你想在你的 shell 脚本中这么做，事情会有所不同。


如果你有一个已最小化过的 JSON 文件，让我来告诉你如何在 Linux 终端中漂亮地输出它。


### 在 Linux 中用 jq 命令漂亮地打印 JSON 文件


[jq](https://stedolan.github.io/jq/) 是一个命令行 JSON 处理器。你可以用它来切分、过滤、映射和转换结构化数据。我在这里不打算详细介绍 `jq` 命令行工具的使用。


要使用 `jq`，你需要先安装它。你可以使用你的[发行版的包管理器](https://itsfoss.com/package-manager/)来安装它。如果启用了 [universe 仓库](https://itsfoss.com/ubuntu-repositories/)，你可以使用 `apt` 命令在 Ubuntu 上安装它：



```
sudo apt install jq

```

安装好后，用下面的方法在显示屏上漂亮地打印 JSON 文件：



```
jq . sample.json

```

![Pretty printed JSON file](/data/attachment/album/202012/13/122430m19h7vehhbokkkzb.png)


你可能也想用 `cat`，但我认为 `cat` 在这里没用。



```
cat sample.json | jq

```

请记住，上述命令不会影响原始 JSON 文件。不会向它写入任何更改。


你可能已经知道[如何在 Linux 中把命令输出重定向到一个文件](https://itsfoss.com/save-command-output-to-file-linux/)。你可能也知道不能重定向到同一个文件，而且 `tee` 命令也不能保证一直有效。


如果你想用漂亮的格式修改原来的 JSON 文件，可以把解析后的输出结果用管道传送到一个新的文件中，然后覆盖原来的 JSON 文件。



```
jq . sample.json > pretty.json

```

![Pretty printing JSON file in Linux Terminal](/data/attachment/album/202012/13/122431nptpmd9auttx96p9.png)


#### 额外技巧：用 jq 命令对 JSON 文件最小化。


让我们反过来，对一个格式良好的 JSON 文件进行最小化。要最小化 JSON 文件，你可以使用选项 `-c`。



```
jq -c < pretty.json

```

![Minified JSON file display](/data/attachment/album/202012/13/122431ikfdfdwf4m3wmlk1.png)


如果你愿意，你也可以使用 `cat` 和重定向：



```
cat pretty.json | jq -c

```

### 在 Linux 中使用 Python 来漂亮地打印 JSON 文件


你更有可能是在系统中安装了 Python。如果是这样的话，你可以用它在终端漂亮地打印 JSON 文件：



```
python3 -m json.tool sample.json

```

![Pretty printing JSON with Python](/data/attachment/album/202012/13/122431pkd8ppj4js8duzds.png)


我知道还有其他方法可以解析 JSON 文件并以适当的格式打印出来。你可以自己去探索，但这两种方法足以完成漂亮地打印 JSON 文件的工作。




---


via: <https://itsfoss.com/pretty-print-json-linux/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

[JSON](https://www.json.org/?ref=its-foss) files are awesome because they store collection of data in a human-readable format. However, reading the JSON file can be a pain if the JSON file is minified.

Take this for an example:

![Print Json](https://itsfoss.com/content/images/wordpress/2020/12/print-json.png)

A computer can easily read it. Even a human can still read it but if JSON file is properly formatted to display the content, it will be much easier. I mean JSON files are supposed to read like this after all:

![Pretty Printed Json](https://itsfoss.com/content/images/wordpress/2020/12/pretty-printed-json.png)

You can use most text editors with some plugins to display it with proper formatting. However, things will be different if you are stuck to a terminal or want to do it in your shell script.

If you got a minified file, let me show you how to pretty print the JSON file in Linux terminal.

## Pretty print JSON with jq command in Linux

[jq](https://stedolan.github.io/jq/?ref=its-foss) is a command line JSON processor. You can use it to slice, filter, map and transform structured data. I am not going into details about using jq command line tool here.

To use jq, you need to install it first. You can use your [distribution’s package manager](https://itsfoss.com/package-manager/) to install it. With [universe repository enabled](https://itsfoss.com/ubuntu-repositories/), you can install it on Ubuntu using the apt command:

`sudo apt install jq`


Once you have it installed, use it in the following manner to pretty print JSON file on the display:

`jq . sample.json`


![Pretty Print Json Linux Terminal](https://itsfoss.com/content/images/wordpress/2020/12/pretty-print-json-linux-terminal.png)

You may also tempt to use the cat command but I believe it is useless use of cat command.

`cat sample.json | jq`


Keep in mind that the above command will not impact the original JSON file. No changes will be written to it.

You probably already know [how to redirect the command output to a file in Linux](https://itsfoss.com/save-command-output-to-file-linux/). You probably also know that you cannot redirect to the same file and the tee command is not guaranteed to work all the time.

If you want to modify the original JSON file with pretty print format, you can pipe the parsed output to a new file and then copy it to the original JSON file.

`jq . sample.json > pretty.json`


![Pretty Printing Json Linux Terminal](https://itsfoss.com/content/images/wordpress/2020/12/pretty-printing-json-linux-terminal.png)

### Bonus: Minify a JSON file with jq command

Let’s take a reverse stance and minify a well-formatted JSON file. To minify a JSON file, you can use the compact option -c.

`jq -c < pretty.json`


![Minify Json File Linux](https://itsfoss.com/content/images/wordpress/2020/12/minify-json-file-linux.png)

You can also use cat and redirection if you want:

`cat pretty.json | jq -c`


## Using Python to pretty print JSON file in Linux

It’s more likely that you have Python installed on your system. If that’s the case, you can use it pretty print the JSON file in the terminal:

`python3 -m json.tool sample.json`


![Pretty Print Json With Python](https://itsfoss.com/content/images/wordpress/2020/12/pretty-print-json-with-python.png)

I know there are other ways to parse JSON file and print it in a proper format. You may explore them on your own but these two are sufficient to do the job, which is to pretty print JSON files.