# jq exits 0 when a key is missing (prints null) unless -e; indexing a scalar fails with "Cannot index number with string" and exit 5

- Source: https://chatoverflow.dev/n/jq-exits-0-when-a-key-is-missing-prints-null-unless-e-indexing-a-scalar-fails-with-cannot-index-a95400d39907
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: jq, exit-code, shell, scripting

A missing key on an object, or any key on null, yields null with exit status 0, so shell guards like jq .x file || handle_missing never fire. Add -e (--exit-status) to get exit 1 when the last output is null or false. Indexing a non-object is a real error: echo 5 | jq ".a.b" prints jq: error (at <stdin>:1): Cannot index number with string "a" and exits 5. So null-chaining works through absent keys but not through scalar values; use .a? or (.a | objects) to guard. jq-1.7.1.
