mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-10 22:21:11 +08:00
commit
370d926ab8
@ -1,114 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to Pretty Print JSON File in Linux Terminal)
|
||||
[#]: via: (https://itsfoss.com/pretty-print-json-linux/)
|
||||
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
|
||||
|
||||
How to Pretty Print JSON File in Linux Terminal
|
||||
======
|
||||
|
||||
[JSON][1] 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:
|
||||
|
||||
![Minified JSON is difficult to read][2]
|
||||
|
||||
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 is easier to read][3]
|
||||
|
||||
You can use most text editor with some plugins to display it with proper formatting. However, if you are stuck to a terminal or if you want to do it in your shell script, things will be different.
|
||||
|
||||
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][4] is a command line JSON processor. You can use it to slice, filter, map and transform structured data. I am not going in 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][5] to install it. With [universe repository enabled][6], 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 printed JSON file][7]
|
||||
|
||||
You may also tempt to use cat but I believe it one of the 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][8]. 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 file in Linux Terminal][9]
|
||||
|
||||
#### 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
|
||||
```
|
||||
|
||||
![Minified JSON file display][10]
|
||||
|
||||
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 printing JSON with Python][11]
|
||||
|
||||
I know there are other ways to parse JSON file and print it with proper format. You may explore them on your own but these two are sufficient to do the job which is to pretty print JSON file.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/pretty-print-json-linux/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.json.org
|
||||
[2]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/12/print-json.png?resize=759%2C253&ssl=1
|
||||
[3]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/12/pretty-printed-json.png?resize=696%2C538&ssl=1
|
||||
[4]: https://stedolan.github.io/jq/
|
||||
[5]: https://itsfoss.com/package-manager/
|
||||
[6]: https://itsfoss.com/ubuntu-repositories/
|
||||
[7]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/12/pretty-print-json-linux-terminal.png?resize=750%2C557&ssl=1
|
||||
[8]: https://itsfoss.com/save-command-output-to-file-linux/
|
||||
[9]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/12/pretty-printing-json-linux-terminal.png?resize=750%2C576&ssl=1
|
||||
[10]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/12/minify-json-file-linux.png?resize=777%2C253&ssl=1
|
||||
[11]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/12/pretty-print-json-with-python.png?resize=777%2C557&ssl=1
|
@ -0,0 +1,114 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to Pretty Print JSON File in Linux Terminal)
|
||||
[#]: via: (https://itsfoss.com/pretty-print-json-linux/)
|
||||
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
|
||||
|
||||
如何在 Linux 终端上漂亮地打印 JSON 文件
|
||||
======
|
||||
|
||||
[JSON][1] 文件非常棒,因为它们以人类可读的格式存储数据集合。然而,如果 JSON 文件被最小化过,那么阅读 JSON 文件可能会很痛苦。
|
||||
|
||||
以这个为例:
|
||||
|
||||
![Minified JSON is difficult to read][2]
|
||||
|
||||
计算机可以很容易地读取它。即使是人也能读懂,但如果 JSON 文件以合适的格式显示,那么阅读就会简单很多。我的意思是 JSON 文件应该是这样读的:
|
||||
|
||||
![Pretty Printed JSON is easier to read][3]
|
||||
|
||||
你可以使用大多数的文本编辑器和一些插件以合适的格式显示它。然而,如果你在终端中,或者你想在你的 shell 脚本中这么做,事情会有所不同。
|
||||
|
||||
如果你有一个已最小化的文件,让我来告诉你如何在 Linux 终端中漂亮地打印 JSON 文件。
|
||||
|
||||
### 在 Linux 中用 jq 命令漂亮地打印 JSON 文件
|
||||
|
||||
[jq][4] 是一个命令行 JSON 处理器。你可以用它来切分、过滤、映射和转换结构化数据。我在这里不打算详细介绍 jq 命令行工具的使用。
|
||||
|
||||
要使用 jq,你需要先安装它。你可以使用你的[发行版的包管理器][5]来安装它。如果启用了 [universe 仓库][6],你可以使用 apt 命令在 Ubuntu 上安装它:
|
||||
|
||||
```
|
||||
sudo apt install jq
|
||||
```
|
||||
|
||||
安装好后,用下面的方法在显示屏上漂亮地打印 JSON 文件:
|
||||
|
||||
```
|
||||
jq . sample.json
|
||||
```
|
||||
|
||||
![Pretty printed JSON file][7]
|
||||
|
||||
你可能也想用 cat,但我认为 cat 在这里没用。
|
||||
|
||||
```
|
||||
cat sample.json | jq
|
||||
```
|
||||
|
||||
请记住,上述命令不会影响原始 JSON 文件。不会向它写入任何更改。
|
||||
|
||||
你可能已经知道[如何在 Linux 中把命令输出重定向到一个文件][8]。你可能也知道不能重定向到同一个文件,而且 tee 命令也不能保证一直有效。
|
||||
|
||||
如果你想用漂亮的格式修改原来的 JSON 文件,可以把解析后的输出结果用管道传送到一个新的文件中,然后覆盖原来的 JSON 文件。
|
||||
|
||||
```
|
||||
jq . sample.json > pretty.json
|
||||
```
|
||||
|
||||
![Pretty printing JSON file in Linux Terminal][9]
|
||||
|
||||
#### 额外技巧:用 jq 命令对 JSON 文件最小化。
|
||||
|
||||
让我们反过来,对一个格式良好的 JSON 文件进行最小化。要最小化 JSON 文件,你可以使用选项 -c。
|
||||
|
||||
```
|
||||
jq -c < pretty.json
|
||||
```
|
||||
|
||||
![Minified JSON file display][10]
|
||||
|
||||
如果你愿意,你也可以使用 cat 和重定向:
|
||||
|
||||
```
|
||||
cat pretty.json | jq -c
|
||||
```
|
||||
|
||||
### 在 Linux 中使用 Python 来漂亮地打印 JSON 文件
|
||||
|
||||
你更有可能是在系统中安装了 Python。如果是这样的话,你可以用它在终端漂亮地打印 JSON 文件:
|
||||
|
||||
```
|
||||
python3 -m json.tool sample.json
|
||||
```
|
||||
|
||||
![Pretty printing JSON with Python][11]
|
||||
|
||||
我知道还有其他方法可以解析 JSON 文件并以适当的格式打印出来。你可以自己去探索,但这两种方法足以完成漂亮地打印 JSON 文件的工作。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/pretty-print-json-linux/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.json.org
|
||||
[2]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/12/print-json.png?resize=759%2C253&ssl=1
|
||||
[3]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/12/pretty-printed-json.png?resize=696%2C538&ssl=1
|
||||
[4]: https://stedolan.github.io/jq/
|
||||
[5]: https://itsfoss.com/package-manager/
|
||||
[6]: https://itsfoss.com/ubuntu-repositories/
|
||||
[7]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/12/pretty-print-json-linux-terminal.png?resize=750%2C557&ssl=1
|
||||
[8]: https://itsfoss.com/save-command-output-to-file-linux/
|
||||
[9]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/12/pretty-printing-json-linux-terminal.png?resize=750%2C576&ssl=1
|
||||
[10]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/12/minify-json-file-linux.png?resize=777%2C253&ssl=1
|
||||
[11]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/12/pretty-print-json-with-python.png?resize=777%2C557&ssl=1
|
Loading…
Reference in New Issue
Block a user