jq if-then-else and default values
jq has a full if expression and a few shortcuts for the most common case: “use this, or a fallback if it is missing”.
if … then … else … end
Every if needs an else (jq 1.7+ allows leaving it out, which then passes the input through) and ends with end.
.[] | "\(.name): " + (if .age >= 40 then "senior" elif .age >= 30 then "mid" else "junior" end)Raw outputTry itInput
[
{
"name": "Ada",
"age": 36,
"team": "core"
},
{
"name": "Linus",
"age": 28,
"team": "kernel"
},
{
"name": "Grace",
"age": 45,
"team": "core"
},
{
"name": "Ken",
"age": 28,
"team": "kernel"
}
]Output
Ada: mid
Linus: junior
Grace: senior
Ken: juniorjq -r '.[] | "\(.name): " + (if .age >= 40 then "senior" elif .age >= 30 then "mid" else "junior" end)' data.jsonDefaults: //
a // b gives b when a is null, false or produces nothing.
.users[] | {name, firstOrder: (.orders[0].id // "none")}CompactTry itInput
{
"users": [
{
"id": 1,
"name": "Ada",
"email": "ada@example.com",
"active": true,
"country": "UK",
"tags": [
"admin",
"dev"
],
"orders": [
{
"id": "o1",
"total": 30
},
{
"id": "o2",
"total": 12.5
}
]
},
{
"id": 2,
"name": "Linus",
"email": "linus@example.com",
"active": false,
"country": "FI",
"tags": [
"dev"
],
"orders": []
},
{
"id": 3,
"name": "Grace",
"email": "grace@example.com",
"active": true,
"country": "US",
"tags": [
"dev",
"ops"
],
"orders": [
{
"id": "o3",
"total": 99
}
]
}
]
}Output
{"name":"Ada","firstOrder":"o1"}
{"name":"Linus","firstOrder":"none"}
{"name":"Grace","firstOrder":"o3"}jq -c '.users[] | {name, firstOrder: (.orders[0].id // "none")}' data.jsonCheck the type
type returns "null", "boolean", "number", "string", "array" or "object". Shortcuts like strings and numbers keep only that type.
map(type)CompactTry itInput
[1, "a", null, [2], {"b": 3}, true]Output
["number","string","null","array","object","boolean"]jq -c 'map(type)' data.json[.[] | numbers]CompactTry itInput
[1, "a", null, 2.5]Output
[1,2.5]jq -c '[.[] | numbers]' data.jsonHandle errors: try … catch
try stops an error from ending the program. Add catch to output something instead.
.[] | try tonumber catch "not a number"CompactTry itInput
["42", "abc", "7.5"]Output
42
"not a number"
7.5jq -c '.[] | try tonumber catch "not a number"' data.jsonSkip values: empty
empty produces no output — useful in an if branch to drop a value.
[.[] | if .age < 30 then .name else empty end]CompactTry itInput
[
{
"name": "Ada",
"age": 36,
"team": "core"
},
{
"name": "Linus",
"age": 28,
"team": "kernel"
},
{
"name": "Grace",
"age": 45,
"team": "core"
},
{
"name": "Ken",
"age": 28,
"team": "kernel"
}
]Output
["Linus","Ken"]jq -c '[.[] | if .age < 30 then .name else empty end]' data.jsonCommon mistakes
//also replacesfalse. For a boolean that may be false, useif .x == null then default else .x end.- Forgetting
endgives a syntax error pointing at the end of the filter.
Practice
Open the jq Playground with your own JSON — it runs the real jq 1.8 in your browser, so nothing is uploaded. The jq cheat sheet lists every filter from these lessons on one page.