Skip to content
Latchkey

jq has() : Test Whether a Key Exists

jq has(k) returns true if the input object contains key k (or the array has index k).

API responses vary by permission and plan; has() lets a step branch on whether an optional field is present before reading it.

What it does

has("name") returns true when the input object has that key, false otherwise. On an array, has(2) tests for that index. It checks presence only, so a key whose value is null still counts as present.

Common usage

Terminal
# keep only items that have a "license" field
jq '.[] | select(has("license"))' repos.json
# branch on an optional field
jq 'if has("parent") then "fork" else "source" end' repo.json
# test an array index
echo '[1,2,3]' | jq 'has(2)'

Functions

FormWhat it does
has("k")True if object has key k
has(2)True if array has index 2
in({...})True if input key is in the given object
contains(x)Deep containment test (different semantics)

Common errors in CI

"jq: error: Cannot check whether null has a string key" means the input was null, not an object; index the parent safely first. "jq: error: Cannot index array with string \"k\"" appears when you call has("name") on an array; use a numeric index for arrays.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →