Skip to content

A JSONPath miss falls back to the default; an error never does

The rule

Normative: this is the rule
  1. JSONPath evaluation MUST distinguish a miss from an error.
  2. A query that runs cleanly and matches nothing is a miss: it yields the default the caller supplied, null where none was supplied, and the evaluation MUST report that nothing matched.
  3. 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).
  4. A query that matches a null MUST yield null and 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.

What the query that matches nothing returnsfallback
"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

Recorded under OPEN-25, OPEN-26, OPEN-40.

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
      }
    }

    fixtures/presence/lang-match-null-delivered.json

  • 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
      }
    }

    fixtures/presence/lang-miss-delivers-default.json

Rule identifiers are permanent and are never renumbered. Each implementation publishes its own standing against these rules; this specification does not.spec 1.0 · LANG-9 · changed in spec 1.0