How to Install and Use jaq (jq clone) on Linux: Step-by-Step Guide

Linux TLDR
Last Updated:
Reading time: 3 minutes

Jaq (pronounced like β€œJacques”) is a Michael Farber and community effort to increase speed, correctness, and simplicity compared to its predecessor, the jq (JSON data processing) tool.

At the moment, it promises to be 30 times faster and already provides improved conditional behavior, resolves crashes, and fixes some programming issues that still exist in jq.

I’ve personally been using it, and aside from performance improvements and fixing programming issues, its compatibility with the original jq syntax is pretty solid. Until now, I haven’t encountered any issues that would make me pull my hair out.

So, if your major work revolves around jq, then I suggest making a switch by reading this article to learn installation steps (available for Linux, Windows, and macOS) and usage examples.

Tutorial Details

DescriptionJaq: A jq clone focused on correctness, speed, and simplicity.
Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityLinux, macOS, and Windows
Prerequisites–
Internet RequiredYes (for installation)

How to Install jaq on Linux, Windows, and macOS

Jaq is widely available on most operating systems, such as Linux, Windows, and macOS. So, if you are running any of these systems with Rust installed, then execute the following command:

$ cargo install jaq

Alternatively, you can also install it using Homebrew on Linux and macOS.

$ brew install jaq

A single executable binary file for Windows is available on the project release page.

How to Use jaq

The β€œjaq” syntax and usage are pretty much the same as β€œjqβ€œ. So, if you’re skilled or good at using β€œjqβ€œ, then just replace β€œjq” with β€œjaq” and keep the rest of the optional parameters the same, and you are good to go.

However, for those who stumble upon this article accidentally and find a tool they have been looking for, I have a few examples for you catered specifically to beginners. So, let’s begin with…

1. Access the key value in the JSON object.

#Access the value of the first key.
$ echo '{"a": 1, "b": 2, "c": 3}' | jaq '.b'

#Access the value of the "y" key in the nested.
$ echo '{"a": 1, "b": 2, "c": {"x": 3, "y": 4}}' | jaq '.c.y'

Output:

get the key value in json

2. Add the values of all keys (with their latest update) in the JSON object.

#The output will be the addition of keys "a", "b", and "c" values.
$ echo '{"a": 1, "b": 2, "c": 3}' | jaq add

#The output will be the addition of key "b" and the updated key "a".
$ echo '{"a": 1, "b": 2, "a": 3}' | jaq add

Output:

adding new key value to json

3. Create two arrays from the JSON data, then match their values; if they are equal, return true; otherwise, return false.

#Return true, as both arrays will be equal. 
$ echo '{"a": 1, "b": 2, "c": 3}' | jaq '[.a, .b, .c] == [.[]]'

#Return false as the first array is not equal to the second array.
$ echo '{"a": 1, "b": 2}' | jaq '[.a, .b, .c] == [.[]]'

#Return true as the first array now matches the second array. 
$ echo '{"a": 1, "b": 2}' | jaq '[.a, .b] == [.[]]'

#Return true as the new JSON values added to match both arrays.
$ echo '{"a": 1, "b": 2}' | jq '. + {"c": 3}' | jaq '[.a, .b, .c] == [.[]]'

Output:

cloning and matching the json data in array

4. Read the value in the array and get the average of its elements.

$ echo '1 2 3 4 5' | jaq -s 'add / length'

Output:

finding an average number of array

5. Read the value in the array as the given value and calculate its percentage with the total value of 120.

$ echo '20 16 8 12 16 18' | jaq -s 'add / 120 * 100'

Output:

finding an percentage from the values in array

6. Repeatedly apply a filter to itself and output the calculated results:

#Each iteration will be incremented by one value until it reaches less than 5.
$ echo '0' | jaq '[recurse(.+1; . < 5)]'

#Each iteration will be incremented by one value and multiplied by 2 until it reaches 20.
$ echo '0' | jaq '[recurse(.+1*2; . <= 20)]'

Output:

applying filters to each iteration in array

7. Transform the JSON data by applying a map function to each element.

$ echo '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]' | jaq 'map(. * 2)'

Output:

applying function to each element

8. Sort a JSON object based on a specific key value.

$ echo '[{"name": "David", "age": 30}, {"name": "Ben", "age": 25}]' | jaq 'sort_by(.age)'

Output:

sorting json data based on specific data

9. Perform conditional operations for calculating the age using specific key values.

$ echo '[{"name": "David", "age": 30}, {"name": "Ben", "age": 25}, {"name": "Thomas", "age": 17}]' | jaq '[.[] | if .age >= 18 then "Adult" else "Kid" end]'

Output:

conditional operation using specific data in json object

10. Split a specific key value based on a specific character.

$ echo '[{"name": "David_Redfield"}, {"name": "Ben Thomas"}]' | jaq 'map(.name | split("_") | join(" "))'

Output:

spliting json data with specific character

I think ten examples would probably be enough since this command is almost identical to β€œjqβ€œ, so you can easily find documentation for jq and just replace it with the command β€œjaqβ€œ. That’s all.

How to Remove jaq

To remove the jaq from your system, simply execute one of the following commands based on your installation method:

#Installed via Cargo, run:
$ cargo uninstall jaq

#Installed via Brew, run:
$ brew uninstall jaq

If you have any questions or queries, then do let me know in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.

1 thought on β€œHow to Install and Use jaq (jq clone) on Linux: Step-by-Step Guide”