JSONPath Multiple Results — Working with Result Arrays
Understand how JSONPath returns multiple results as arrays. Learn to work with multi-value expressions using wildcards, recursive descent, and filters.
Detailed Explanation
Multiple Results in JSONPath
One of the fundamental concepts in JSONPath is that expressions can return multiple values. Unlike simple key lookups that return a single value, many JSONPath operators produce result arrays containing zero, one, or many matches.
Operators That Return Multiple Results
$.users[*].name // wildcard — all user names
$..email // recursive descent — all emails at any depth
$.items[0:5] // slice — first five items
$.data[?(@.active)] // filter — all active records
$["a", "b"] // union — values for keys a and b
Example
Given:
{
"team": [
{ "name": "Alice", "role": "lead", "active": true },
{ "name": "Bob", "role": "dev", "active": true },
{ "name": "Carol", "role": "dev", "active": false },
{ "name": "Dave", "role": "qa", "active": true }
]
}
| Expression | Result |
|---|---|
$.team[*].name |
["Alice", "Bob", "Carol", "Dave"] |
$.team[?(@.active==true)].name |
["Alice", "Bob", "Dave"] |
$.team[0:2].role |
["lead", "dev"] |
$..role |
["lead", "dev", "dev", "qa"] |
Result Set Characteristics
- Ordered — results maintain the order in which they appear in the document.
- May contain duplicates — if the same value appears multiple times, it shows up multiple times in the result.
- Homogeneous or mixed — results can be all the same type or a mix of strings, numbers, objects, and arrays.
Empty Results
When no elements match, the result is an empty array [], not an error. This makes JSONPath safe to use with optional or variable-structure data.
Processing Multiple Results
After extracting a result array, common operations include:
- Count: check the length to see how many matches exist
- First/Last: use index
[0]or[-1]on the result - Transform: map over results to reshape the data
- Aggregate: calculate sum, average, or other statistics
Use Case
Understanding multiple results is essential when querying arrays of records from APIs — for example, extracting all product names from a catalog, collecting all error codes from a batch response, or listing all unique tags across blog posts.