Missing Fields and null in jq
jq prints null for an absent key and for a key whose value is JSON null because both serialize the same way, so a plain path lookup cannot tell them apart. Use has(key) or keys to test presence, and remember that the // operator replaces null and false rather than testing whether a key exists.
The short answer
jq prints null for a missing key because a path expression on an object yields null when the key is absent, and JSON null is also a value. The output alone cannot distinguish the two. Test presence with has("key"), which returns true only when the key exists, even if its value is null.
A minimal example
Given this input:
{"name": "Ada", "nickname": null}
both nickname and a key that does not exist serialize as null:
echo '{"name":"Ada","nickname":null}' \
| jq '{name: .name, nickname: .nickname, email: .email}'
{"name":"Ada","nickname":null,"email":null}
Presence testing separates the two:
echo '{"name":"Ada","nickname":null}' \
| jq '{name: has("name"), nickname: has("nickname"), email: has("email")}'
{"name":true,"nickname":true,"email":false}
nickname exists and holds null; email does not exist. Both facts survive only because has asks about the key, not the value.
Why this happens
jq follows JSON, which has no undefined or missing-value literal. A path lookup has to return something, so jq returns null for an absent member. Indexing null also yields null, which means a chain like .user.email cannot fail partway through the way a property access in many programming languages would.
echo '{"user": null}' | jq '.user.email' # null
echo '{}' | jq '.user.email' # null
That permissiveness is convenient for optional fields but hides data problems. An empty object, a missing intermediate object, and an explicit null all produce the same output. Use type to see what a value is, and has to see whether a member is there.
Arrays behave differently. Indexing an array beyond its length returns null, while indexing an array with a string key is an error. has(5) reports whether index 5 exists, so the same presence test works for arrays with numeric arguments.
Telling absence from null
| Expression | {"a": null} |
{} |
Notes |
|---|---|---|---|
.a |
null |
null |
Cannot distinguish |
has("a") |
true |
false |
Presence test |
keys |
["a"] |
[] |
Whole-object view |
.a // "default" |
"default" |
"default" |
Also replaces false |
.a? |
null |
null |
Suppresses errors only |
The alternative operator // is not a presence test. It substitutes its right side when the left side produces null, false, or no value at all, so it treats an explicit null, a missing key, and a false value alike. Use it to fill display defaults, not to audit which fields exist.
To keep explicit nulls while dropping absent keys:
jq 'map(select(has("email")))' users.json # keeps {"email": null}
jq 'map(select(.email != null))' users.json # drops {"email": null} too
A small helper makes the policy explicit:
def get_or($key; $default):
if has($key) then .[$key] else $default end;
{"email": null} | {email: get_or("email"; "unknown")} returns {"email":null} on purpose: the key is present, so its value is used unchanged.
Edge cases
has requires an object or array. Calling it on a string or number raises an error, so filter by shape before testing:
select(type == "object") | if has("id") then .id else empty end
getpath(["a","b"]) also returns null when either segment is missing, so it does not preserve the distinction. Check each level with has, or wrap the lookup in try.
The ? operator is not a presence test either. .a? on an object still yields null for a missing key; on a string it yields no result instead of an error. That empty result stream is a third state, distinct from both null and a present value. empty and select work on that stream, which is why map(select(has("email"))) drops objects instead of emitting nulls.
Nulls in output also hide which field was optional when you build a new object. If downstream code needs to know, carry the presence bit in the output: {email: .email, email_present: has("email")}. That is more useful than a sentinel string that could collide with a real value.
For a wider comparison of null handling across query languages, see how SQL NULL testing works.
Sources
Revision history
- Published
- Updated
- — Initial publication.