How to Pretty Print JSON file in Linux Shell Script

1. Install Python:

The first step is to install Python on your Linux system. Python is a programming language that can be used to write scripts for automating tasks.

2. Install the JSON Module:

Once Python is installed, you can install the JSON module. This module provides functions for encoding and decoding JSON data.

3. Create a Script:

Create a script that will read the JSON file and pretty print it. You can use the json.dumps() function to pretty print the JSON data.

4. Execute the Script:

Finally, execute the script to pretty print the JSON file. You can do this by running the script from the command line.
[ad_1]

JSON (JavaScript Object Notation) is a lightweight data-interchange format that has become increasingly popular in recent years due to its simplicity, readability, and flexibility. It is commonly used in web applications, APIs, and data storage.

When dealing with JSON files in Linux Shell Script, it is essential to have a tool that can format the data in a human-readable way. This process is known as “pretty printing” and can be achieved using various command-line tools. In this tutorial, you will learn some of the popular ways to pretty print a JSON file in Linux Shell Script.

How to Pretty Print JSON file in Linux Shell Script

By using these tools, you can make it easier to work with JSON files to print pretty json and ensure that your data is always easy to read and understand.

  • Method 1: Using jq Command
  • Method 2: Using Python’s json.tool
  • Method 3: Using Node.js’s json Command
  • Method 4: Using the json_xs command to pretty print JSON file

Method 1: Using jq Command

jq is a lightweight and flexible command-line JSON processor that allows you to manipulate, filter, and format JSON data. It is available for all major operating systems and can be installed easily using the package manager.

To pretty print a JSON file using the jq command, simply run the following command in your terminal:

jq . filename.json

This will format the JSON data in a human-readable way and display it in the terminal. You can also save the output to a file by redirecting the output to a new file, as shown below:

jq . filename.json > output.json

Method 2: Using Python’s json.tool

Python is a popular programming language that comes with a built-in JSON module that provides a simple way to encode and decode JSON data. The json.tool module is a useful utility that can pretty print JSON data in the terminal.

To use json.tool to pretty print a JSON file, simply run the following command:

python -m json.tool filename.json

This will format the JSON data in a human-readable way and display it in the terminal. You can also save the output to a file by redirecting the output to a new file, as shown below:

python -m json.tool filename.json > output.json

Method 3: Using Node.js’s json Command

Node.js is a popular JavaScript runtime that can be used to manipulate and format JSON data. The json command is a simple utility that allows you to pretty print JSON data in the terminal.

To use the json command to pretty print a JSON file, simply run the following command:

cat filename.json | json

This will format the JSON data in a human-readable way and display it in the terminal. You can also save the output to a file by redirecting the output to a new file, as shown below:

cat filename.json | json > output.json

Method 4: Using the json_xs command to pretty print JSON file

The json_xs command is a part of the JSON Perl module and can be used to encode and decode JSON data. It is available on most Linux systems and can be installed using the package manager.

Step 1: Install JSON Perl Module

The json_xs command is a part of the JSON Perl module, so you need to make sure it is installed on your system before you can use it. To install the JSON Perl module on your Linux system, use the following command:

sudo apt-get install libjson-perl

This will install the JSON Perl module and all its dependencies on your system.

Step 2: Pretty Print JSON Files with json_xs Command

Once you have installed the JSON Perl module, you can use the json_xs command to pretty print JSON files. To use the json_xs command to pretty print a JSON file, simply run the following command:

json_xs -f filename.json -t pretty

This will format the JSON data in a more readable way and display it in the terminal. You can also save the output to a new file by redirecting the output to a new file, as shown below:

json_xs -f filename.json -t pretty > output.json

The -f option specifies the input file name, and the -t option specifies the output format. In this case, the -t option is set to “pretty,” which formats the output in a more readable way.

Conclusion

In this tutorial, you have learned some of the popular ways to pretty print a JSON file in Linux Shell Script. Whether you prefer using a command-line tool like jq or a programming language like Python or Node.js, there are many options available to format your JSON data in a human-readable way. By using these tools, you can make it easier to work with JSON files and ensure that your data is always easy to read and understand.

Recommended Tutorials

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00