Tools
Get Pro

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 it

Input

[
  {
    "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: junior
jq -r '.[] | "\(.name): " + (if .age >= 40 then "senior" elif .age >= 30 then "mid" else "junior" end)' data.json

Defaults: //

a // b gives b when a is null, false or produces nothing.

.users[] | {name, firstOrder: (.orders[0].id // "none")}CompactTry it

Input

{
  "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.json

Check the type

type returns "null", "boolean", "number", "string", "array" or "object". Shortcuts like strings and numbers keep only that type.

map(type)CompactTry it

Input

[1, "a", null, [2], {"b": 3}, true]

Output

["number","string","null","array","object","boolean"]
jq -c 'map(type)' data.json
[.[] | numbers]CompactTry it

Input

[1, "a", null, 2.5]

Output

[1,2.5]
jq -c '[.[] | numbers]' data.json

Handle errors: try … catch

try stops an error from ending the program. Add catch to output something instead.

.[] | try tonumber catch "not a number"CompactTry it

Input

["42", "abc", "7.5"]

Output

42
"not a number"
7.5
jq -c '.[] | try tonumber catch "not a number"' data.json

Skip values: empty

empty produces no output — useful in an if branch to drop a value.

[.[] | if .age < 30 then .name else empty end]CompactTry it

Input

[
  {
    "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.json

Common mistakes

  • // also replaces false. For a boolean that may be false, use if .x == null then default else .x end.
  • Forgetting end gives 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.