A JSONPath miss falls back to the default; an error never does
The rule
- JSONPath evaluation MUST distinguish a miss from an error.
- A query that runs cleanly and matches nothing is a miss: it yields the default the caller supplied,
nullwhere none was supplied, and the evaluation MUST report that nothing matched. - A query that cannot be compiled or cannot be run is an error: it MUST be reported as one, and MUST NOT yield the default (LANG-22, LANG-29).
- A query that matches a
nullMUST yieldnulland report a match, never the default.
What it means
A JSONPath query can come back empty-handed in two quite different ways: it
runs cleanly and matches nothing, or it never runs at all because the string
does not compile or the engine fails. Only the first is a miss. A miss hands
back whatever default the caller supplied, null if none was given, and says
that nothing matched. An error is reported as an error and never turns into the
default, so a typo in a query cannot pass for data that simply is not there.
What the caller can also always tell is whether the query matched: a query that
matches a stored null returns that null and reports a match, never the
default.
Example
Two queries against the same data, with the same supplied default:
$.store.nonexistent, which compiles and runs but matches nothing, and
$.[invalid, which cannot compile.
"fallback"The query that cannot compile returns no value at all: it is reported as an error, and a node evaluating it fails (LANG-22).
Why
Fixtures
Test cases whose outcome this rule decides. A case compares absent, null and empty members exactly; the fixtures README defines each operation. Where a case and the rule disagree, the rule is right.
A query that matches null delivers null and reports a match extract
{ "given": { "data": { "a": null }, "path": "$.a", "default": "fallback" }, "expect": { "value": null, "success": true } }A query that matches nothing delivers the default and reports a miss extract
{ "given": { "data": { "a": 1 }, "path": "$.b", "default": "fallback" }, "expect": { "value": "fallback", "success": false } }