Return the input unchanged
The identity filter is the smallest useful jq program.
.Input and expected output
{"hello":"world"}{
"hello": "world"
}Executable reference
Search 43 focused examples by task or syntax. Every recipe includes sample input, exact output, and a one-click path into the workbench.
The identity filter is the smallest useful jq program.
.{"hello":"world"}{
"hello": "world"
}Chain object identifiers to walk through nested objects.
.user.profile.name{"user":{"profile":{"name":"Ada"}}}"Ada"Use bracket notation when a key is not a valid jq identifier.
.["first-name"]{"first-name":"Ada"}"Ada"The optional operator suppresses an error when the input cannot be indexed.
.users[] | .profile?.email?{"users":[{"profile":{"email":"a@example.test"}},null]}"a@example.test"
nullA missing object field normally yields null; ? is most useful when the input type may differ.
.[] is a generator: one input can produce many output values.
.users[]{"users":["Ada","Grace","Linus"]}"Ada"
"Grace"
"Linus"Array indexes are zero-based; negative indexes count from the end.
.users[-1]{"users":["Ada","Grace","Linus"]}"Linus"A slice includes the start index and excludes the end index.
.[1:4][0,1,2,3,4][
1,
2,
3
]The right side runs once for every value emitted on the left.
.users[] | .name{"users":[{"name":"Ada"},{"name":"Grace"}]}"Ada"
"Grace"The comma operator sends both filters the same input.
.id, .name{"id":42,"name":"Ada"}42
"Ada"Array construction collects all emitted values into one array.
[.users[].name]{"users":[{"name":"Ada"},{"name":"Grace"}]}[
"Ada",
"Grace"
]select emits its input only when the condition is truthy.
.users[] | select(.active){"users":[{"name":"Ada","active":true},{"name":"Linus","active":false}]}{
"name": "Ada",
"active": true
}map applies a filter to each item and collects the results.
.prices | map(. * 1.2){"prices":[10,25,40]}[
12,
30,
48
]map_values updates values while retaining their keys.
.stock | map_values(. + 1){"stock":{"apples":2,"pears":4}}{
"apples": 3,
"pears": 5
}has distinguishes an absent key from a key whose value is null.
has("email"){"name":"Ada","email":null}truecontains recursively tests whether the input contains another value.
contains({"roles":["admin"]}){"name":"Ada","roles":["admin","editor"]}trueBuild a new object and use shorthand when the key and field name match.
{id, displayName: .profile.name}{"id":7,"profile":{"name":"Ada"},"private":true}{
"id": 7,
"displayName": "Ada"
}Put an expression in parentheses to compute an object key.
{(.key): .value}{"key":"status","value":"ready"}{
"status": "ready"
}\(filter) inserts a filter result into a jq string.
.users[] | "\(.name): \(.role)"{"users":[{"name":"Ada","role":"admin"}]}"Ada: admin"|= feeds the current value through a filter and writes the result back.
.users |= map(. + {active: true}){"users":[{"name":"Ada"},{"name":"Grace"}]}{
"users": [
{
"name": "Ada",
"active": true
},
{
"name": "Grace",
"active": true
}
]
}// returns the right side when the left produces false, null, or no value.
.nickname // "Anonymous"{"name":"Ada","nickname":null}"Anonymous"Boolean false also activates the fallback.
type reports jq types such as object, array, number, and string.
map(type)[null,true,12,"jq",[],{}][
"null",
"boolean",
"number",
"string",
"array",
"object"
]keys returns sorted object keys; keys_unsorted keeps approximate insertion order.
keys{"z":1,"a":2,"m":3}[
"a",
"m",
"z"
]with_entries exposes each property as {key, value}, then rebuilds the object.
with_entries(.key |= ascii_downcase){"FirstName":"Ada","ROLE":"Admin"}{
"firstname": "Ada",
"role": "Admin"
}del removes one or more paths from a copied result.
del(.password, .token){"name":"Ada","password":"secret","token":"abc"}{
"name": "Ada"
}sort_by evaluates a key expression for every array item.
sort_by(.score) | reverse[{"name":"Ada","score":92},{"name":"Grace","score":98},{"name":"Linus","score":85}][
{
"name": "Grace",
"score": 98
},
{
"name": "Ada",
"score": 92
},
{
"name": "Linus",
"score": 85
}
]group_by sorts values by the key and collects equal keys together.
group_by(.team) | map({team: .[0].team, members: map(.name)})[{"name":"Ada","team":"compiler"},{"name":"Grace","team":"compiler"},{"name":"Linus","team":"kernel"}][
{
"team": "compiler",
"members": [
"Ada",
"Grace"
]
},
{
"team": "kernel",
"members": [
"Linus"
]
}
]unique_by keeps one value for each distinct key and sorts by that key.
unique_by(.id)[{"id":2,"name":"Grace"},{"id":1,"name":"Ada"},{"id":2,"name":"Duplicate"}][
{
"id": 1,
"name": "Ada"
},
{
"id": 2,
"name": "Grace"
}
]add reduces an array using jq addition.
.orders | map(.total) | add{"orders":[{"total":12.5},{"total":7.5},{"total":20}]}40max_by returns the item whose key expression is greatest.
max_by(.score)[{"name":"Ada","score":92},{"name":"Grace","score":98}]{
"name": "Grace",
"score": 98
}split creates an array; join combines its values with a separator.
.path | split("/") | map(ascii_upcase) | join(" › "){"path":"docs/guides/jq"}"DOCS › GUIDES › JQ"test returns a boolean and accepts optional regex flags.
.users[] | select(.email | test("@example\.com$"; "i")) | .name{"users":[{"name":"Ada","email":"ada@EXAMPLE.com"},{"name":"Grace","email":"grace@test.dev"}]}"Ada"capture returns an object whose keys are the named groups.
capture("(?<major>[0-9]+)\.(?<minor>[0-9]+)")"version 1.8"{
"major": "1",
"minor": "8"
}gsub replaces all non-overlapping regex matches.
gsub("[^a-zA-Z0-9]+"; "-") | ascii_downcase"Hello, jq world!""hello-jq-world-"as binds a value while letting the remaining pipeline keep its original input.
. as $root | .users[] | {name, team: $root.team}{"team":"compiler","users":[{"name":"Ada"},{"name":"Grace"}]}{
"name": "Ada",
"team": "compiler"
}
{
"name": "Grace",
"team": "compiler"
}Conditions are filters and must end with end.
if .score >= 90 then "excellent" elif .score >= 70 then "pass" else "retry" end{"score":84}"pass"try/catch turns an error into a value you can handle deliberately.
try (.value | tonumber) catch "not a number"{"value":"unknown"}"not a number"paths emits path arrays; scalars limits them to leaf-like values.
[paths(scalars)]{"user":{"name":"Ada","active":true},"count":2}[
[
"user",
"name"
],
[
"user",
"active"
],
[
"count"
]
].. walks every descendant; objects selects only values that can have a name field.
.. | objects | .name? // empty{"name":"root","children":[{"name":"first"},{"nested":{"name":"second"}}]}"root"
"first"
"second"reduce folds a stream into a single accumulated value.
reduce .users[] as $user ({}; .[$user.id] = $user.name){"users":[{"id":"u1","name":"Ada"},{"id":"u2","name":"Grace"}]}{
"u1": "Ada",
"u2": "Grace"
}@csv safely quotes array values as a CSV row; raw output removes jq string quotes.
.users[] | [.id, .name, .email] | @csv{"users":[{"id":1,"name":"Ada","email":"ada@example.test"},{"id":2,"name":"Grace, G.","email":"grace@example.test"}]}1,"Ada","ada@example.test"
2,"Grace, G.","grace@example.test"Raw whole-text mode passes the editor contents as one jq string.
split("\n") | map(select(length > 0)) | lengthalpha
beta
gamma
3Null-input mode runs the filter once with null and ignores the input editor.
[range(1; 6) | {id: ., square: (. * .)}](ignored)[
{
"id": 1,
"square": 1
},
{
"id": 2,
"square": 4
},
{
"id": 3,
"square": 9
},
{
"id": 4,
"square": 16
},
{
"id": 5,
"square": 25
}
]Compact output writes one JSON value per line; sorted keys make object order predictable.
.{"z":1,"a":{"y":2,"b":3}}{"a":{"b":3,"y":2},"z":1}