JSONPath · lesson 8 of 13 · Filters
Comparisons
[?condition] keeps the children for which the condition is true. Inside it, @ stands for the child being tested.
Examples
Example 1Cheap books
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[?@.price < 10].title
[?Start of a filter: test every element ofbook.@.price@is the element being tested; read itsprice.< 10The comparison —==,!=,<,<=,>and>=are available.
Result
"Sayings of the Century" "Moby Dick"
Example 2Comparing text
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[?@.category == 'fiction'].title@.category == 'fiction'Strings are quoted, with single or double quotes.
Result
"Sword of Honour" "Moby Dick" "The Lord of the Rings"
Good to know
- Parentheses around the condition are allowed:
[?(@.price < 10)]means the same.