Home·Terminal commands

jq

Text processing

Usage

jq '[filter]' [file.json]

Filters and reshapes JSON with its own small language; it is often not installed by default, and -r is what strips the quotes from string output.

Common flags

FlagMeaning
-rRaw output, so strings come out without quotes
-cOne compact line per result
-eSet the exit status from the result, for scripts
--argPass a shell string in as a jq variable
-sSlurp the whole input into one array
-nStart from null input and build output yourself

Examples

curl -s api/users | jq -r '.[].name'

Every name, unquoted, one per line.

jq '.items | length' data.json

Counts the entries in an array.

jq -c '.[] | {id, name}' big.json

Two fields per row, compact.

These were built to be piped together. Three joined commands are usually shorter than one that does everything.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does jq do?

Filters and reshapes JSON with its own small language; it is often not installed by default, and -r is what strips the quotes from string output.

Q. How do I type it?

jq '[filter]' [file.json] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man jq. This command sits under Text processing.

Related commands

man page: man jq