jq cheatsheet

Select & transform

Filter values and reshape arrays or objects. Explore 5 examples, then try one against the included input or your own data.

jq examples

5 examplesFilter values and reshape arrays or objects.
Select & transform

Keep matching objects

select emits its input only when the condition is truthy.

.users[] | select(.active)
Input and expected output
Input
{"users":[{"name":"Ada","active":true},{"name":"Linus","active":false}]}
Expected output
{
  "name": "Ada",
  "active": true
}
Select & transform

Transform every array item

map applies a filter to each item and collects the results.

.prices | map(. * 1.2)
Input and expected output
Input
{"prices":[10,25,40]}
Expected output
[
  12,
  30,
  48
]
Select & transform

Transform every object value

map_values updates values while retaining their keys.

.stock | map_values(. + 1)
Input and expected output
Input
{"stock":{"apples":2,"pears":4}}
Expected output
{
  "apples": 3,
  "pears": 5
}
Select & transform

Check whether a key exists

has distinguishes an absent key from a key whose value is null.

has("email")
Input and expected output
Input
{"name":"Ada","email":null}
Expected output
true
Select & transform

Check whether a value is contained

contains recursively tests whether the input contains another value.

contains({"roles":["admin"]})
Input and expected output
Input
{"name":"Ada","roles":["admin","editor"]}
Expected output
true