• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

jq: command not found (JSON CLI Parser)

by admin

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.

Filed Under: Linux

Some more articles you might also be interested in …

  1. The Squid Service in Failed State with Error: “Failed to make swap directory /var/spool/squid/00: (13) Permission denied”
  2. Common Init.ora Parameters and Unix, Linux Kernel Parameters and Relationship Between Them
  3. How to disable lvmetad in CentOS/RHEL
  4. fstrim Command Examples in Linux
  5. uuid Command Examples in Linux
  6. fprintd-enroll Command Examples in Linux
  7. How systemd-tmpfiles cleans up /tmp/ or /var/tmp (replacement of tmpwatch) in CentOS / RHEL 7
  8. lrzuntar Command Examples in Linux
  9. grpck command – Remove corrupt or duplicate entries in the /etc/group and /etc/gshadow files.
  10. asciidoctor: A processor that converts AsciiDoc files to a publishable format

You May Also Like

Primary Sidebar

Recent Posts

  • aws ec2: CLI for AWS EC2 (Command Examples)
  • aws cur – Create, query, and delete AWS usage report definitions (Command Examples)
  • aws configure – Manage configuration for the AWS CLI (Command Examples)
  • aws cognito-idp: Manage Amazon Cognito user pool and its users and groups using the CLI

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright