Skip to content

A single JSONPath match is unwrapped unless the path selects many

Arity is read off the path, not off the result, so a query written to select many keeps a list shape even on the day it matches exactly one thing.

The rule

Normative: this is the rule
  1. A JSONPath query that returns exactly one match MUST yield that match rather than a one-element array, unless the path is written to select many, containing a wildcard, a filter or a slice.
  2. A path written to select many MUST yield an array, even when only one item matched.

What it means

Whether a result comes back as a bare value or as an array is decided by reading the query's own syntax, not by counting how many things it happened to match today. A query with no wildcard, filter or slice in it is written to find one thing, so a single match is handed back directly. A query that carries one of those — even when, on a given record, it only ever matches one item — keeps its array shape regardless, because the query was written to select many and the day it matches one is not a promise it always will.

Example

Two queries against the same stored data: $.store.name, an ordinary path, and $.store.books[?(@.price < 9)].title, a filter written to select many — each matching exactly one item.

What the ordinary path returnsunwrapped
"Test Store"
What the filter path returnslist
["Book 3"]

Why

Recorded under OPEN-25.

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-15 · changed in spec 1.0