logo

jq Cheatsheet

Last Updated: 2024-01-30

Basic Info

Select

$ jq '.name' file.json

# used with pipes
$ cat file.json | jq '.a.b[0].c'

# pretty print
$ cat file.json | jq

Multiple fields:

# from top level
$ cat file.json | jq '. | {field1, field2}'

# from sub level
$ cat file.json | jq '.results[] | {id, name}'

Extract as text values instead of JSON (getting rid of quotes), use -r:

$ cat file.json | jq -r '.a.b[0].c'

Slice an array

$ echo '["a", "b", "c", "d"]' | jq '.[1:3]'

Filter

# get "name" of id 1
$ cat file.json | jq '.results[] | select(.id == "1") | {name}'

# get result with multiple filters
$ cat file.json | jq '.results[] | select((.id == "1") and (.name="a"))'

# filter with substrings
$ cat file.json | jq '.results[] | select(.name | contains("ab"))'

# filter with regex
$ cat file.json | jq '.results[] | select(.name | test("Joe\s+Smith"))'

Functions

Selectors start with .. Functions do not.

$ echo '{ "a": 1, "b": 2 }' | jq 'keys' # [a, b]
  • keys: get keys of the objects.
  • length: get the length of the array or the number of the keys. (e.g. select non-empty arrays select(.my_array | length > 0))
  • flatten: flatten nested arrays.
  • unique: get the unique values of an array.
  • join: join the elements using a separator (e.g. cat file.json | jq '[.a, .b] | join(" ")'))

Create new objects

$ echo '{ "a": 1, "b": 2 }' | jq '{ a: .b, b: .a }'
# { "a": 2, "b": 1 }

Delete Values

$ jq 'del(.somekey)' input.json

Extract Raw

Use -r to extract raw text values instead of JSON:

$ jq -r '.tags | .[] | select(contains("1.0.0")) | length'

Example: Harbor

# Get repositories (in a JSON array)
$ curl -u $USERNAME:$PASSWORD https://$IP:$PORT/v2/_catalog | jq -r '.repositories'

# Get repositories (in a raw list)
$ curl -u $USERNAME:$PASSWORD https://$IP:$PORT/v2/_catalog | jq -r '.repositories | .[]'

# Get number of repositories
$ curl -u $USERNAME:$PASSWORD https://$IP:$PORT/v2/_catalog | jq -r '.repositories | length'

More Examples

# base 64 decode
... | jq -r '.data.password | @base64d'

# jq remove prefix and substitute, e.g. https://10.200.0.19:10443=>10.200.0.19.10443
... | jq -r 'ltrimstr("https://") | sub(":";".")'

# field name with `.`
... | jq -r '.data."ca.crt"'

Install jq

# macOS
brew install jq

# Debian / Ubuntu
sudo apt install jq

Other platforms: https://jqlang.github.io/jq/download/