A filter is how JSONPath selects items by a condition instead of by position: "the books under 10", "the users that have an email". It is the most useful part of the language and the part where implementations disagree most, so it is worth knowing exactly how it behaves.
The examples here use the standard bookstore document with four books, and every result below was produced by running the expression, not written from memory. You can paste the document into the JSONPath tester and try each one.
The shape of a filter
A filter is written in square brackets with a question mark, and applies a condition to each child of the array or object before it:
$.store.book[?(@.price < 10)]
$is the root of the document.@is the item currently being tested.- The expression in the parentheses is evaluated for every book. The books for which it is true are selected.
In the current standard, RFC 9535, the parentheses are optional: [?@.price < 10] is the same filter.
Note what it returns: the matching books, not their prices. To get a field, continue the path.
$.store.book[?(@.price < 10)].title
→ ["Sayings of the Century", "Moby Dick"]
Comparisons
The operators are ==, !=, <, <=, > and >=. Strings are written in quotes, and either single or double quotes work.
$.store.book[?(@.category == 'fiction')].title
$.store.book[?(@.price >= 12.99)].title
Ordering comparisons only succeed between two numbers or two strings. @.price > 5 does not match an item whose price is the string "10"; comparing across types is false rather than an error.
Combining conditions
Use && for and, || for or, ! for not, and parentheses to group:
| Expression | Result |
|---|---|
$.store.book[?(@.category == 'fiction' && @.price > 10)].title |
Sword of Honour, The Lord of the Rings |
$.store.book[?(@.price < 9 || @.price > 20)].title |
Sayings of the Century, Moby Dick, The Lord of the Rings |
Testing whether a property exists
A bare path is true if the path finds something:
$.store.book[?(@.isbn)].title
→ ["Moby Dick", "The Lord of the Rings"]
$.store.book[?(!@.isbn)].title
→ ["Sayings of the Century", "Sword of Honour"]
This tests for existence, not truthiness. An isbn whose value is null or 0 still exists and passes. If you need to exclude nulls, compare explicitly: @.isbn != null.
Comparing with another part of the document
Inside a filter, $ still means the document root, so you can compare an item with a value from elsewhere:
$..book[?(@.price < $.store.bicycle.price)].title
→ ["Sayings of the Century", "Sword of Honour", "Moby Dick"]
This is the three books cheaper than the bicycle at 19.95.
Filtering the members of an object
A filter also works on the members of an object, not only on the items of an array. The store object has two members, the array of books and the bicycle:
$.store[?(@.price)].color
→ ["red"]
Only the bicycle has a price, so only it passes. It is also the reason $.store[?(@.price < 10)] returns nothing: the children of store are the book array and the bicycle, not the individual books. To filter the books, step down to the array first, as in the earlier examples.
Testing inside an array
The standard has no "contains" operator, but a filter can hold another path as an existence test, and that path can have a filter of its own. Take a list of products with tags: a mug tagged sale and kitchen, a lamp tagged home, and a desk tagged sale and office.
$.items[?(@.tags[?(@ == 'sale')])].name
→ ["Mug", "Desk"]
The inner filter looks through each item's tags for the string sale, and the item passes if it finds anything.
Three mistakes that return nothing, or an error
- Comparing across types.
$.store.book[?(@.price < "10")]compares a number with a string, which is false for every book. The result is an empty list and nothing tells you why. - Filtering the wrong level. As shown above, a filter tests the children of whatever came before it. Check what is actually one step up.
- Quotes and operators. An unquoted word such as
@.category == fictionis not a string comparison, and a single=is not an operator. Strict implementations reject both, and the tester here reportsExpected ")"for the single=.
Functions
RFC 9535 defines a small set of functions that can appear in filters: length(), count(), match(), search() and value(). The common one is length(), which gives the length of a string, array or object:
$.store.book[?(length(@.title) > 20)].title
→ ["Sayings of the Century", "The Lord of the Rings"]
match() and search() test a string against a regular expression and are how the standard replaces the older =~ operator. The tester on this site supports length() but not the regular-expression functions, and says so when you use them.
Where implementations differ
If a filter works in one tool and returns nothing in another, it is usually one of these:
- Older libraries support things the standard does not, such as
=~for regular expressions and script expressions like[(@.length-1)]. An expression that depends on them will not run in a strict implementation. - Missing properties. The standard defines what a comparison with a missing property does. Some older libraries treat it as an error or as
null. - Whether a filter reaches inside nested arrays. A filter tests the immediate children of the array or object before it. To search at any depth, put a recursive step first:
$..[?(@.price < 10)]. - Quotes and escaping. Single quotes are standard, but some libraries are strict about one form.
Because of this, test an expression in the library that will run it. If you are choosing a tool in the first place, JSONPath versus jq explains which job each is built for.