How to install jq on Ubuntu? [100% Working]


Tips and Tricks, Ubuntu

Author: Omer Cakmak
Reviewer: Deepak Prasad

JSON; It is a text format that facilitates the exchange of structured data between all programming languages. It is useful in many contexts and applications with its notation of spring brackets, square brackets, colons, and commas.

The jq is a command line based JSON processor that allows to transform, filter, slice, map, or perform other operations on JSON data.

In this article, we will explain the installation of the JQ package on Ubuntu, a Debian Based operating system.

 

Different methods to install jq on Ubuntu

Let's see the installation of the JQ package from the Ubuntu repositories, then install the package manually.

 

Method-1: Install from Repository

foc@ubuntu22:~$ sudo apt search jq

jq/jammy 1.6-2.1ubuntu3 amd64
lightweight and flexible command-line JSON processor

This output shows a version of 1.6-2. Latest version 1.6 on the official github page. Now let's upload this version to the Ubuntu:

foc@ubuntu22:~$ sudo apt install jq -y
foc@ubuntu22:~$ jq --version
jq-1.6

 

Method-2: Install Manually

If you want to manual install JQ package, go to packages.ubuntu.com and search the JQ package.

install jq

 

You can see the package of that version by clicking the Ubuntu versions.

How to install jq on Ubuntu? [100% Working]

For example, there is a 1.6-2 version in the Ubuntu Jammy Repository, click and download.

foc@ubuntu22desktop:~/Downloads$ ls
jq_1.6-2.1ubuntu3_amd64.deb

Then install with the command below:

foc@ubuntu22desktop:~/Downloads$ sudo apt install ./jq_1.6-2.1ubuntu3_amd64.deb  -y

or

foc@ubuntu22desktop:~/Downloads$ sudo dpkg -i jq_1.6-2.1ubuntu3_amd64.deb 
Selecting previously unselected package jq.
(Reading database ... 208739 files and directories currently installed.)
Preparing to unpack jq_1.6-2.1ubuntu3_amd64.deb ...
Unpacking jq (1.6-2.1ubuntu3) ...
Setting up jq (1.6-2.1ubuntu3) ...
Processing triggers for man-db (2.10.2-1) ...

Version:

foc@ubuntu22desktop:~/Downloads$ jq --version
jq-1.6

 

A JQ example

Example-1: Let's create a data sequence in Json format:

foc@ubuntu22desktop:~$ echo '{ "user":"foc", "author":"Ömer Çakmak", "website":"www.golinuxcloud.com"  }' | jq

Output:

{
  "user": "foc",
  "author": "Ömer Çakmak",
  "website": "www.golinuxcloud.com"
}

 

Example-2: Now let's write data in JSON format to a file and display the data in this file with jq:

foc@ubuntu22desktop:~$ echo '{"status":"success","data":[{"name":"Omer","age":28},{"name":"Faruk","age":34}]}' > example.json

Then run this file:

foc@ubuntu22desktop:~$ jq "." example.json 
{
  "status": "success",
  "data": [
    {
      "name": "Omer",
      "age": 28
    },
    {
      "name": "Faruk",
      "age": 34
    }
  ]
}

The jq tool supports various filters that can be applied on JSON data. The absolute simplest filter is "."(dot) . This is a filter that takes its input and produces it unchanged as output. That is, this is the identity operator.

 

Example-3: Now let's pull information in JSON format from a website:

foc@ubuntu22desktop:~$ curl httpbin.org/get
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.81.0", 
    "X-Amzn-Trace-Id": "Root=1-63825b26-315496b66c9eb9e2547e0ea1"
  }, 
  "origin": "78.184.105.61", 
  "url": "http://httpbin.org/get"
}

If we want to get headers information from incoming information:

foc@ubuntu22desktop:~$ curl httpbin.org/get | jq .headers
...
{
  "Accept": "*/*",
  "Host": "httpbin.org",
  "User-Agent": "curl/7.81.0",
  "X-Amzn-Trace-Id": "Root=1-63825bb9-13a0073122246ac373096e91"
}

In this way, you can select the part you want to receive from the incoming data.

 

Summary

Manual installation can be preferred as a temporary solution. However, our advice is to install from the package repository of the Ubuntu version you use.

For more information about JQ you can use the --help parameter:

foc@ubuntu22desktop:~$ jq --help
jq - commandline JSON processor [version 1.6]

Usage:	jq [options] <jq filter> [file...]
	jq [options] --args <jq filter> [strings...]
	jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

...

Example:

	$ echo '{"foo": 0}' | jq .
	{
		"foo": 0
	}

Some of the options include:
  -c               compact instead of pretty-printed output;
  -n               use `null` as the single input value;
  -e               set the exit status code based on the output;
  -s               read (slurp) all inputs into an array; apply filter to ...

For JQ filters and more information, you can get help from the manual page:

foc@ubuntu22desktop:~$ man jq

You will also find filter examples on this page.

 

References

stackoverflow.com - Install jq JSON processor on Ubuntu 10.04

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment