Table of Contents
In this article, we will see how to install jq(JSON processor) on Ubuntu 22.04. jq is a free and open source command line JSON processor used for parsing json data. It is much more like sed, awk or grep utility where you can use it to slice and filter the structured data. jq is developed in Portable C language and does not have any dependencies. jq is widely popular and used by number of developers to format JSON files in a "pretty" format. It can be easily used to with other linux/unix tools in a command or in a script using piping mechanism.
jq is mostly favored for array processing because of its ability to traverse through a complex array to get the data. It is currently available to be installed on almost all the famous platforms. Here we will see the steps to install jq utility on Ubuntu 22.04.
How to Install jq(JSON processor) on Ubuntu 22.04
Also Read: How to Install TFLint (Terraform Linter) on Linux
Step 1: Prerequisites
a) You should have a running Ubuntu 22.04
Server.
b) You should have sudo
or root
access to run privileged commands.
c) You should have apt
or apt-get
and dpkg
utility available in your Server.
d) You should have snap
utility installed in your server in case jq is required to be installed as snap package.
Step 2: Update Your Server
It is always a good idea to first look for all the latest available updates from default ubuntu repo and install them by using sudo apt update && sudo apt upgrade
command as shown below.
cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
Hit:1 https://artifacts.elastic.co/packages/8.x/apt stable InRelease
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu jammy InRelease
Hit:4 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:5 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:6 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:7 https://ngrok-agent.s3.amazonaws.com buster InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
alsa-ucm-conf grub-efi-amd64-bin grub-efi-amd64-signed
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
Step 3: Install jq
In the next step, you can install jq utility by choosing any of the below methods depending on your needs and requirements.
a) Using Apt
The most easiest way to install jq package is from default Ubuntu repo by running sudo apt install jq
command as shown below. This will download and install the package along with all its dependencies.
cyberithub@ubuntu:~$ sudo apt install jq [sudo] password for cyberithub: Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libjq1 libonig5 The following NEW packages will be installed: jq libjq1 libonig5 0 upgraded, 3 newly installed, 0 to remove and 3 not upgraded. Need to get 357 kB of archives. After this operation, 1,087 kB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libonig5 amd64 6.9.7.1-2build1 [172 kB] Get:2 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 libjq1 amd64 1.6-2.1ubuntu3 [133 kB] Get:3 http://in.archive.ubuntu.com/ubuntu jammy/main amd64 jq amd64 1.6-2.1ubuntu3 [52.5 kB] Fetched 357 kB in 2s (209 kB/s) Selecting previously unselected package libonig5:amd64. (Reading database ... 234352 files and directories currently installed.) Preparing to unpack .../libonig5_6.9.7.1-2build1_amd64.deb ... Unpacking libonig5:amd64 (6.9.7.1-2build1) ... Selecting previously unselected package libjq1:amd64. Preparing to unpack .../libjq1_1.6-2.1ubuntu3_amd64.deb ... Unpacking libjq1:amd64 (1.6-2.1ubuntu3) ... Selecting previously unselected package jq. Preparing to unpack .../jq_1.6-2.1ubuntu3_amd64.deb ... Unpacking jq (1.6-2.1ubuntu3) ... Setting up libonig5:amd64 (6.9.7.1-2build1) ... Setting up libjq1:amd64 (1.6-2.1ubuntu3) ... Setting up jq (1.6-2.1ubuntu3) ... Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.3) ...
b) Using Snap
Another method that you can choose to install jq utility is from the snap store. If you have snap utility available in your server then you can use sudo snap install jq
command to install jq as snap package.
cyberithub@ubuntu:~$ sudo snap install jq jq 1.5+dfsg-1 from Michael Vogt (mvo*) installed
Step 4: Verify Installation
If you installed jq package from Ubuntu repo using apt package manager then you can check the installed files path by using dpkg -L jq
command as shown below.
cyberithub@ubuntu:~$ dpkg -L jq
/.
/usr
/usr/bin
/usr/bin/jq
/usr/share
/usr/share/doc
/usr/share/doc/jq
/usr/share/doc/jq/AUTHORS.gz
/usr/share/doc/jq/README
/usr/share/doc/jq/copyright
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/jq.1.gz
/usr/share/doc/jq/changelog.Debian.gz
Step 5: Check Version
You can also check the current installed version by using jq --version
command as shown below.
cyberithub@ubuntu:~$ jq --version jq-1.6
Step 6: Using jq
Now that jq utility is successfully installed in the server, let's use it to perform few json operations. We will see few examples from GitHub which has a JSON API. If you would like to check the first 4 commit done on a repo, say atlantis-example
in our case then you can check it by using below curl query.
cyberithub@ubuntu:~$ curl 'https://api.github.com/repos/cyberithub/atlantis-example/commits?per_page=4'
It is possible that output of above query may not be readable properly. In that case, you can pipe the output to jq utility to show it in pretty format as shown below.
cyberithub@ubuntu:~$ curl 'https://api.github.com/repos/cyberithub/atlantis-example/commits?per_page=4' | jq '.'
Now if you are looking to check the first commit done then you can extract that data by running below command.
cyberithub@ubuntu:~$ curl 'https://api.github.com/repos/cyberithub/atlantis-example/commits?per_page=4' | jq '.[0]' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 14208 0 14208 0 0 26507 0 --:--:-- --:--:-- --:--:-- 26458 { "sha": "835146f16381163e992e9570r5k61f43a6fa9eae", "node_id": "C_kwDOHBPFrdoAKDgzNTE0NmAzIju4PTE7E2U5OWJlODQ2MGQ3ZjYxZjQzYTZmYTllYWU", "commit": { "author": { "name": "CyberITHub", "email": "admin@cyberithub.com", "date": "2023-05-23T15:44:07Z" }, "committer": { "name": "CyberITHub", "email": "admin@cyberithub.com", "date": "2023-05-23T15:44:07Z" }, "message": "Minor changes", "tree": { "sha": "4f6d0e9a8419865086acj320e45e3f7e9ee398d7", "url": "https://api.github.com/repos/cyberithub/atlantis-example/git/trees/5j6e0e9a8417173856abk980e98e3a6e7ee165d7" }, "url": "https://api.github.com/repos/cyberithub/atlantis-example/git/commits/976137e16381163e992e8460d7f61f43a5fa9eae", "comment_count": 0, "verification": { "verified": false, "reason": "unsigned", "signature": null, "payload": null } }, ....................
If you are looking for some specific information then instead of getting entire output, you can look to extract only those information. For example, if you would like to extract the message
and name
from the first commit then you have to use below curl
command.
cyberithub@ubuntu:~$ curl 'https://api.github.com/repos/cyberithub/atlantis-example/commits?per_page=4' | jq '.[0]' | jq '{message: .commit.message, name: .commit.committer.name}' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 14208 100 14208 0 0 25974 0 --:--:-- --:--:-- --:--:-- 25927 { "message": "Minor changes", "name": "CyberITHub" }
Step 7: Uninstall jq
Once you are done with jq utility, you can choose to uninstall the package from your system by using any of the below methods depending on how it is installed.
a) Using Apt
If you installed jq tool from default Ubuntu repo then to uninstall run sudo apt remove jq
command as shown below. If you are also looking to remove all the dependencies along with the package then you have to use --auto-remove
option with the below command.
cyberithub@ubuntu:~$ sudo apt remove jq Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages were automatically installed and are no longer required: libjq1 libonig5 Use 'sudo apt autoremove' to remove them. The following packages will be REMOVED: jq 0 upgraded, 0 newly installed, 1 to remove and 3 not upgraded. After this operation, 102 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 234368 files and directories currently installed.) Removing jq (1.6-2.1ubuntu3) ... Processing triggers for man-db (2.10.2-1) ...
b) Using Snap
If you installed jq as snap package then to remove you can use sudo snap remove jq
command as shown below.
cyberithub@ubuntu:~$ sudo snap remove jq jq removed