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
| Flag | Meaning |
|---|---|
| -r | Raw output, so strings come out without quotes |
| -c | One compact line per result |
| -e | Set the exit status from the result, for scripts |
| --arg | Pass a shell string in as a jq variable |
| -s | Slurp the whole input into one array |
| -n | Start 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.jsonCounts the entries in an array.
jq -c '.[] | {id, name}' big.jsonTwo 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