How to Install the JQ Command in Linux?

Learn via video courses
Topics Covered

JQ is a lightweight and powerful command-line tool designed specifically for working with JSON data in Linux environments. It allows users to parse, filter, extract, and manipulate JSON files effortlessly, making it an essential utility for developers, system administrators, and data analysts. The working of the JQ tool is based on a pipeline model, where data is processed through a series of transformations.

It consists of the following components,

  • The JSON Parser is responsible for reading and parsing JSON data input. It validates and converts the raw JSON input into an internal representation that JQ can work with.
  • The Query Compiler processes the query or program provided to JQ. This component compiles the query into an intermediate representation known as JQ bytecode.
  • The JQ bytecode generated by the Query Compiler is executed by the Virtual Machine. The VM acts as an interpreter, interpreting and executing the bytecode instructions to perform the desired operations on the JSON data.
  • The Filters and transformations allow users to extract specific data, filter out unwanted elements, modify values, and perform complex computations on the JSON data.
  • Once the desired operations have been performed on the JSON data, the Output Formatter takes the processed data and converts it back into a readable format.

The JQ command in Linux can be installed using the recommended package manager method or directly from the JQ website. Follow step 1 to install the JQ tool in your system through the command line and step 2 to install it from the website,

1. You can use the given command based on your Linux distro to install the JQ tool

  • For Debian/Ubuntu-based systems:

  • For Fedora/RHEL-based systems:

If your Linux system is openSUSE or Arch distro or you want to use another package manager or want to install the tool on FreeBSD or Solaris, you can find the relative command and instructions through the Download Page of JQ package.

2. Follow the following steps to install the JQ tool manually,

  • Visit the official JQ GitHub repository using a web browser.

  • Download the latest stable release of JQ under the Assets section.

  • Once the download is complete, open a terminal and navigate to the directory where the downloaded file is located.

  • Extract the downloaded archive using the following command:

    Replace the <version> with your installed version number. The current version is 1.6 and your command will be tar xvf jq-1.6.tar.gz.

  • Move into the extracted directory,

  • Configure, compile, and install JQ using the following commands:

    You can also download the binary file directly from the Download Page of the JQ package and follow the following steps to install the JQ tool from the installed file,

  • Go to the downloads folder and xxtract the downloaded file using the following command.

  • Move into the extracted directory and move the jq binary file to the /usr/local/bin directory to be accessible system-wide.

After completing the steps, to verify the installation, open a new terminal and type,

To syntax of the jq command in Linux is,

  • The [options] specifies optional flags or options that you can use to modify the behavior of the jq command. For example, you can specify options like --compact-output to generate compact output without whitespace.
  • The JQ filter expression is enclosed in single quotes (''). This filter expression specifies the transformations or operations to be applied to the JSON data.
  • The <input-file> is the path or filename of the input JSON file that you want to process with the JQ command.

Working with Linux JQ Command

Now that you have JQ installed, let's explore some of its key features and how to utilize them effectively. To better understand the working of the jq command, let us consider the following example file data.json and work on it.

Organize JSON Data Using JQ Command

One of the primary functions of JQ is to format JSON output in a readable manner. This feature is particularly useful when dealing with large or complex JSON files. To organize JSON data using JQ, use the following command,

Output:

organize-json-data-using-jq-command

We will continue with this formatted JSON file for the next examples.

Access Properties Using JQ Command

A JSON object has properties like name and email with their respective values. To get a specific value based on properties from the JSON data, you can use the JQ command. For example, to extract the value of the name property,

Output:

access-properties-using-jq-command

If we want to get a value of a property in an object in JSON object. For example, to get the value of name in the country property, the following command can be used,

Output:

access-properties-using-jq-command-1

Access Array Items Using JQ Command

The jq command provides ways to access and manipulate individual items within arrays. To access the second item in the languages array, we can use the following command,

Output:

access-array-items-using-jq-command

Slicing of an Array Using JQ Command

The Linux jq command also supports the slicing of arrays to extract a range of items from the array. For instance, to get the first two items from the languages array:

Output:

slicing-of-an-array-using-jq-command

Filtering JSON Data Using JQ Command

JQ allows you to filter JSON data based on specific criteria. We can use the following functions to manipulate and filter JSON data,

  • The with_entries function in JQ allows you to transform the properties of a JSON object individually. It takes a filter expression as an argument and applies it to each key-value pair in the object.
  • The select function in JQ allows you to filter JSON data based on specific conditions or criteria.

For example, let's filter the JSON to include only the properties where the value is of type string:

Output:

filtering-json-data-using-jq-command

Conditional Transformations Using JQ Command

We can also apply conditional logic to transform JSON data selectively. Suppose we want to add a new property called isAdult based on the person's age on the condition that if the age is greater than or equal to 18, isAdult will be set to true; otherwise, it will be set to false. We can use the + operator along with other functions in the jq query to perform this.

  • The dot (.) represents the current JSON object being processed by JQ.
  • The plus symbol (+) is used for combining or merging objects in JQ.
  • The { "isAdult": (.age >= 18) } is an object literal in JQ syntax where >= is used for condition. It creates a new object with a property called isAdult.

Output:

conditional-transformations-using-jq-command

Mapping Array Elements Using JQ Command

JQ allows you to transform individual elements within an array. Let's convert all the elements of the "languages" array to uppercase:

  • The languages is the name of the array.
  • The [] notation indicates that we want to iterate over each element of the array.
  • The |= operator in JQ is known as the update-assignment operator. It allows us to modify the value of a particular expression or path.
  • The ascii_upcase is the JQ built-in filter that converts a string to uppercase using ASCII characters. It transforms each element of the languages array to its uppercase equivalent.

Output:

mapping-array-elements-using-jq-command

Learn More

If you are interested in expanding your knowledge of Linux commands and enhancing your proficiency in working with the JQ command, consider exploring the following resources:

  • Familiarizing yourself with basic Linux commands will not only complement your understanding of the JQ command but also empower you to navigate and interact with the Linux operating system more effectively.
  • Enrolling in linux reading track can provide structured learning and in-depth knowledge of Linux and its command-line tools.
  • Read more on the underlying architecture of the Linux operating system to deepen your understanding of Linux.

Conclusion

  • The JQ command is an essential tool for working with JSON data in Linux.
  • To install the JQ command, you can use the package manager of your Linux distribution or manually download and compile the latest stable release from the official JQ GitHub repository.
  • Working with the JQ command involves various operations for formatting such as Organizing and Accessing properties or Accessing array items.
  • We can use built-in functions such as select and with_entries for Filtering JSON data and operators for Conditional transformations.
  • The JQ command offers an overall flexible and powerful toolkit for working with JSON data.