jq: command not found (JSON CLI Parser)

jq is a lightweight and flexible command-line JSON processor much like sed in that it enables you to slice, filter, map, and transform data from one format to another. For instance, it can be used to convert JSON data into CSV (comma-separated values) for loading into a non-JSON–columned MySQL database. You can download it from https://stedolan.github.io/jq/, and there is an online version at https://jqplay.org/ for experimentation. Plus, jq uses the Perl Compatible Regular Expressions (PCRE) parser, like many other languages. (This wonderful tool deserves to have much more written about it than the simple examples here, and reading the manual is a quick way to become acquainted with the many features of this tool.)

If you encounter below error:

jq: command not found (JSON CLI Parser)

You may try installing below package as per your choice of distribution.

Distribution Command
OS X brew install jq
Debian apt-get install jq
Ubuntu apt-get install jq
Alpine apk add jq
Arch Linux pacman -S jq
Kali Linux apt-get install jq
Fedora dnf install jq
Raspbian apt-get install jq
Docker docker run cmd.cat/jq jq

With no arguments to jq, it will “pretty print” the JSON document. This is very handy for extremely complex documents with many layers of embedded objects and arrays that are hard to view on a single flat line.

Select Certain Fields

On some occasions, not all the data in a JSON document will be of interest, and you can use jq to reform the data and provide only selected parts. For example:

$ jq '{city, state, id}' myfile.json

jq Command Examples

1. Output a JSON file, in pretty-print format:

$ jq . file.json

2. Output all elements from arrays (or all key-value pairs from objects) in a JSON file:

$ jq .[] file.json

3. Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):

$ jq --slurp . file.json

4. Output the first element in a JSON file:

$ jq .[0] file.json

5. Output the value of a given key of the first element in a JSON text from stdin:

$ cat file.json | jq .[0].key_name

6. Output the value of a given key of each element in a JSON text from stdin:

$ cat file.json | jq 'map(.key_name)'

Summary

jq is a command-line JSON processor. It works like sed for JSON data; you can use it to filter, parse, and transform structured data with the same ease that sed, awk, or grep let you do with raw text. Jq is available on GitHub at https://stedolan.github.io/jq/. The installation is very simple; it’s just a single binary, available for Windows, macOS, and Linux. Just download it and copy it into the folder available on your system PATH to be able to run it from the shell or command line.

Related Post