How to Pretty Print JSON file in Linux Shell Script

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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *