JSONPath · lesson 12 of 13 · Filters
Functions
RFC 9535 defines length(), count(), match(), search() and value() for use inside filters.
Examples
Example 1Long titles
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[?length(@.title) > 15].title
length(@.title)The number of characters in the title.> 15Keep books whose title is longer than 15 characters.
Result
"Sayings of the Century" "The Lord of the Rings"
Example 2Authors matching a pattern
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[?match(@.author, "^[NE].*")].title
match(A regular-expression test that must match the whole string."^[NE].*"Names starting withNorE, followed by anything.
Result
"Sayings of the Century" "Sword of Honour"
Good to know
search(@.title, "the")matches anywhere inside the string;matchneeds the whole string to match.count(@.*)counts how many members an element has:[?count(@.*) > 4]finds the books that carry an ISBN.- Functions only work inside
[? … ]:$.store.book.length()is a syntax error.