JSONPath Filter Expressions — [?(@.key == value)]
Master JSONPath filter expressions to select array elements based on conditions. Learn the ?() syntax, comparison operators, and practical filtering examples.
Detailed Explanation
Filter Expressions in JSONPath
Filter expressions use the [?()] syntax to select array elements or object properties that satisfy a given condition. The @ symbol inside the filter refers to the current element being evaluated.
Syntax
$.array[?(@.property == value)]
$.array[?(@.price < 50)]
$.array[?(@.name)]
Basic Filtering
Given:
{
"books": [
{ "title": "Clean Code", "price": 35, "category": "programming" },
{ "title": "The Hobbit", "price": 12, "category": "fiction" },
{ "title": "Refactoring", "price": 45, "category": "programming" },
{ "title": "Dune", "price": 15, "category": "fiction" }
]
}
$.books[?(@.category == "programming")]returns the Clean Code and Refactoring objects.$.books[?(@.price < 20)]returns The Hobbit and Dune.$.books[?(@.price >= 35)].titlereturns["Clean Code", "Refactoring"].
The @ Symbol
The @ symbol represents the current element in the iteration. Think of it like the loop variable in a for...of loop:
// Conceptually equivalent:
books.filter(item => item.category === "programming")
// JSONPath:
$.books[?(@.category == "programming")]
Supported Operators
| Operator | Example | Meaning |
|---|---|---|
== |
@.status == "active" |
Equal to |
!= |
@.type != "draft" |
Not equal to |
< |
@.age < 18 |
Less than |
<= |
@.score <= 100 |
Less than or equal |
> |
@.price > 0 |
Greater than |
>= |
@.rating >= 4.5 |
Greater than or equal |
Chaining Filters
You can apply a filter and then access a property on the filtered results:
$.books[?(@.category == "programming")].title
This first filters the books array, then extracts the title from each matching element.
Use Case
Use filter expressions to query JSON data like a database — selecting products within a price range, finding users with a specific role, filtering log entries by severity level, or extracting records that match business rules.