How to Fix “jq: Command Not Found (JSON CLI Parser)” Error?

In Linux, the “jq” is the command that is primarily considered for transforming the data into JSON format. It is beneficial for the user and the Linux system to easily understand the data format as JSON and used for parsing and manipulating JSON data. While using the “jq” Command in Linux, the user may face the error “jq: command not found (JSON CLI Parser).”

This post will demonstrate the reason and solution for the error “jq: command not found (JSON CLI Parser).”

Reason 1: Not Installed jq Command 

The first reason for the error “jq command not found” is that the user has not installed the jq command. The error is shown in the following image:

$ curl -s http://api.open-notify.org/iss-now.json | jq .

Solution: Install jq Command

The solution for the error is to install the “jq” command in Linux using the subsequent command based on the Linux distros:

$ sudo apt  install jq           #For Debian/Ubuntu
$ sudo yum install jq            #For Fedora/CentOS/RHEL
$ sudo pacman -Syu jq            #For Arch

The “jq” command has been installed with the dependent packages.

Let’s check and process the data into JSON format through the “jq” command. The below-given command will convert the given author’s name into JSON format as follows:

  • Henry” in the “name 1” position
  • joseph” in the “name 2” position 
  • johnsons” in the “name 3” position
$ echo '{"Authors":{"name 1":"Henry","name 2":"joseph","name 3":"johnsons"}}' | jq '.'

The data has been converted into JSON format.

Reason 2: jq Command is Installed but Not Working

Another possible error case could be the jq command is installed but not working due to any reason. The user can resolve it by re-installing the “jq” command by removing the install jq command.

Solution: Uninstall jq Command and Re-install it

To re-install the jq command in Linux, go through the subsequent steps:

Step 1: Uninstall jq Command

To completely uninstall the jq command is possible via the given command based on the Linux distros:

$ sudo apt purge --auto-remove jq           #For Debian/Ubuntu
$ yum autoremove jq                         #For Fedora/CentOS/RHEL
$ sudo pacman -R jq                         #For Arch

The “jq” command has been removed with all dependent packages.

Step 2: Install jq Command

To install the jq command in Linux, use the command in the above solution.

Verify the Change

Once the “jq” command is re-installed, use the jq command to process the work. As in our case, the authors’ names are converted into JSON format, as done in the above solution.

$ echo '{"Authors":{"name 1":"Henry","name 2":"joseph","name 3":"johnsons"}}' | jq '.'

The given data has been converted into JSON format.

Reading Key Value From JSON File 

Let’s read the JSON File “Authors.json” with the following file content:

$ cat Authors.json   | jq '.'

The array of Authors contains the “name 1”, “name 2”, and “name 3” locations with the value “Henry”, “joseph,” and “johnsons

To read the value of the “name 2” location, the following command will be considered: 

$ jq '.[] | ."name 2"' Authors.json

The value for location “name 2” is “joseph,” as shown above.

Conclusion

The “jq: command not found” is that the jq command is not installed. To fix this error, install the jq command or completely remove the installed jq command and re-install it. This write-up has enlightened the solutions for the error “q: command not found.”