JSONPath · lesson 13 of 13 · Gotchas
Filter, then index
Selectors chain: each one applies to the children of what the previous one matched. So [?…][0] does not mean "the first match".
Examples
Example 1The matches themselves
Document
{
"store": {
"book": [
{ "title": "Sayings of the Century", "author": "Nigel Rees",
"category": "reference", "price": 8.95 },
{ "title": "Sword of Honour", "author": "Evelyn Waugh",
"category": "fiction", "price": 12.99 },
{ "title": "Moby Dick", "author": "Herman Melville",
"category": "fiction", "price": 8.99, "isbn": "0-553-21311-3" },
{ "title": "The Lord of the Rings", "author": "J. R. R. Tolkien",
"category": "fiction", "price": 22.99, "isbn": "0-395-19395-8" }
],
"bicycle": { "color": "red", "price": 19.95 }
}
}Query
$.store.book[[email protected] < 10].title[[email protected] < 10]Matches two book objects;.titlethen reads each one's title.
Result
"Sayings of the Century" "Moby Dick"
Example 2Trying to take the first match
Document
{
"store": {
"book": [
{ "title": "Sayings of the Century", "author": "Nigel Rees",
"category": "reference", "price": 8.95 },
{ "title": "Sword of Honour", "author": "Evelyn Waugh",
"category": "fiction", "price": 12.99 },
{ "title": "Moby Dick", "author": "Herman Melville",
"category": "fiction", "price": 8.99, "isbn": "0-553-21311-3" },
{ "title": "The Lord of the Rings", "author": "J. R. R. Tolkien",
"category": "fiction", "price": 22.99, "isbn": "0-395-19395-8" }
],
"bicycle": { "color": "red", "price": 19.95 }
}
}Query
$.store.book[[email protected] < 10][0].title
[[email protected] < 10]Matches the two cheap book objects…[0]…then looks for index 0 inside each book — an object, which has no index 0 — so nothing is left.
Result
No results.
Good to know
- To get only the first match, take the first item of the result list — in the playground, the first result.