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
Description | Jaq: A jq clone focused on correctness, speed, and simplicity. |
Difficulty Level | Moderate |
Root or Sudo Privileges | No |
OS Compatibility | Linux, macOS, and Windows |
Prerequisites | β |
Internet Required | Yes (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:
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:
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:
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:
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:
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:
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:
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:
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:
10. Split a specific key value based on a specific character.
$ echo '[{"name": "David_Redfield"}, {"name": "Ben Thomas"}]' | jaq 'map(.name | split("_") | join(" "))'
Output:
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!
If you donβt want to build it yourself you could tru cargo -binstall