February 20, 20222 min read

Pretty Print JSON in bash using jq

Install jq. It is a very simple and handy tool to pretty print JSON in a formatted manner. jq is a command-line utility for working with JSON data. JSON (JavaScript Object Notation) is a lightweight data interchange format that is widely used to exchange data between web applications and servers. jq allows you to manipulate, filter, and extract data from JSON files, making it a powerful tool for working with JSON data.

Installation of jq

For linux users-

$ sudo apt-get update
$ sudo apt-get install jq

For MAC users-

$ brew install jq

Pretty Print JSON in Bash using jq

In your bash, you can echo a JSON string and pipe (|) it with jq and you'll see a pretty JSON output.

$ echo '{ "name": "Gary", "email": ""}' | jq

Output will look like-

  "name": "Gary",
  "email": ""

jq can handle large JSON structures without any performance issues. It has been a great tool that I've used over the years.

Pretty Print JSON file in bash using jq

$ jq --color-output . file1.json file1.json | less -R

Any command that would generate a JSON output, it can be piped to print a formatted JSON-

$ command_with_json_output | jq .

Pretty Print API response in bash using jq

An example from the docs, where you can pretty print a API response which is JSON-

$ curl '' | jq '.'

You can also play around in the interactive mode of jq, by just entering jq in the bash.

$ jq

jq can be used on JSON string, files, stream, and more.

Learn more about jq in the official jq tutorial

Using jq is one of the easiest and most convenient way to pretty print a JSON in a bash script.

Share this blog
Tagged in :
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
December 18, 20222 min read
Next Article