Tools
Get Pro

jq select(): filter arrays by value

select(condition) passes its input through when the condition is true and drops it otherwise. Put it after .[] to keep only matching items.

Match a field value

Iterate the array, then keep items whose field equals a value.

.users[] | select(.country == "US") | .nameTry 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

"Grace"
jq '.users[] | select(.country == "US") | .name' data.json

Boolean fields

A boolean field is already a condition, so select(.active) is enough. Use select(.active | not) for the opposite.

.users[] | select(.active) | .nameTry 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

"Ada"
"Grace"
jq '.users[] | select(.active) | .name' data.json
.users[] | select(.active | not) | .nameTry 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

"Linus"
jq '.users[] | select(.active | not) | .name' data.json

Compare numbers

Use >, >=, <, <=, == and !=.

.[] | select(.age > 30) | .nameTry 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"
"Grace"
jq '.[] | select(.age > 30) | .name' data.json

Several conditions: and / or

Combine conditions with and and or. Parentheses make the intent clear.

.[] | select(.team == "kernel" and .age < 30) | .nameTry 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 '.[] | select(.team == "kernel" and .age < 30) | .name' data.json

Keep the result as an array: map(select(...))

.[] | select(...) gives separate results. To get an array back — for example to count matches — use map(select(...)).

map(select(.team == "core"))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

[{"name":"Ada","age":36,"team":"core"},{"name":"Grace","age":45,"team":"core"}]
jq -c 'map(select(.team == "core"))' data.json
map(select(.team == "core")) | lengthTry 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

2
Count the matches. jq 'map(select(.team == "core")) | length' data.json

Match against a list of values

IN(...) tests whether a value is one of several.

.users[] | select(.country | IN("UK", "US")) | .nameTry 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

"Ada"
"Grace"
jq '.users[] | select(.country | IN("UK", "US")) | .name' data.json

Items whose array contains a value

index("x") finds a value in an array; any(...) checks whether any element matches a condition.

.users[] | select(any(.tags[]; . == "ops")) | .nameTry 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

"Grace"
jq '.users[] | select(any(.tags[]; . == "ops")) | .name' data.json

Common mistakes

  • Writing select(.country = "US") — a single = is assignment. Comparison is ==.
  • Comparing a number to a string: "28" == 28 is false. Convert with tonumber or tostring.

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.