In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to print JSON files on Linux terminals". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and study and learn "how to print JSON files on Linux terminals" together.
JSON files are great because they store collections of data in a human-readable format. However, if JSON files are minimized, reading JSON files can be painful.
Take this as an example:
Minified JSON is difficult to read
Computers can easily read it. Even a human can read it, but if the JSON file is displayed in the right format, reading is much easier. I mean, JSON files should read like this:
Pretty Printed JSON is easier to read
You can use most text editors and some plugins to display it in the appropriate format. However, if you are in the terminal, or if you want to do this in your shell script, things will be different.
If you have a minimized JSON file, let me show you how to print it beautifully in a Linux terminal.
Print JSON files beautifully with jq command in Linux
jq is a command-line JSON processor. You can use it to slice, filter, map, and transform structured data. I'm not going to go into detail about using the jq command-line tool here.
To use JQ, you need to install it first. You can install it using your distribution's package manager. If the universe repository is enabled, you can install it on Ubuntu using the apt command:
sudo apt install jq
Once installed, print the JSON file beautifully on the display using the following method:
jq . sample.json
Pretty printed JSON file
You might want to use cat, but I don't think cat works here.
cat sample.json | jq
Keep in mind that the above commands do not affect the original JSON file. No changes are written to it.
You probably already know how to redirect command output to a file in Linux. You may also know that you can't redirect to the same file, and that tee is not guaranteed to work all the time.
If you want to modify the original JSON file in a nice format, you can pipe the parsed output to a new file and overwrite the original JSON file.
jq . sample.json > pretty.json
Pretty printing JSON file in Linux Terminal
Bonus tip: Minimize JSON files with jq command.
Let's reverse and minimize a well-formed JSON file. To minimize JSON files, you can use option-c.
jq -c < pretty.json
Minified JSON file display
You can also use cat and redirect if you like:
cat pretty.json |jq -c Use Python in Linux to print JSON files beautifully
You are more likely to have Python installed on your system. If this is the case, you can use it to print JSON files beautifully on the terminal:
python3 -m json.tool sample.json
Thank you for reading, the above is the content of "how to print JSON files on Linux terminals". After studying this article, I believe everyone has a deeper understanding of how to print JSON files on Linux terminals. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.