JSONPath Comparison Operators — ==, !=, <, >, <=, >=
Complete guide to JSONPath comparison operators for filter expressions. Learn ==, !=, <, >, <=, >= with type coercion rules and practical examples.
Detailed Explanation
Comparison Operators in JSONPath
Comparison operators are used inside filter expressions [?()] to test element values against conditions. JSONPath supports six standard comparison operators that work with strings, numbers, booleans, and null.
Operator Reference
| Operator | Name | Example |
|---|---|---|
== |
Equal | @.status == "active" |
!= |
Not equal | @.role != "guest" |
< |
Less than | @.age < 18 |
> |
Greater than | @.score > 90 |
<= |
Less than or equal | @.price <= 100 |
>= |
Greater than or equal | @.rating >= 4.0 |
Example Data
{
"products": [
{ "name": "Laptop", "price": 999, "rating": 4.5, "inStock": true },
{ "name": "Mouse", "price": 29, "rating": 4.2, "inStock": true },
{ "name": "Monitor", "price": 450, "rating": 4.8, "inStock": false },
{ "name": "Keyboard", "price": 75, "rating": 3.9, "inStock": true }
]
}
Examples
$.products[?(@.price == 29)]— returns Mouse (exact match)$.products[?(@.price != 999)]— returns Mouse, Monitor, Keyboard$.products[?(@.price < 100)]— returns Mouse, Keyboard$.products[?(@.price > 100)]— returns Laptop, Monitor$.products[?(@.rating >= 4.5)]— returns Laptop, Monitor$.products[?(@.inStock == true)]— returns Laptop, Mouse, Keyboard
String Comparisons
String comparison uses lexicographic ordering:
$.products[?(@.name > "Laptop")] // Monitor, Mouse (alphabetically after "Laptop")
$.products[?(@.name == "Mouse")] // exact string match
Type Coercion
Most JSONPath implementations perform strict type comparison:
"5" == 5isfalse(string vs. number)true == 1isfalse(boolean vs. number)null == falseisfalse(null vs. boolean)
Combining Multiple Conditions
Use logical operators to combine comparisons:
$.products[?(@.price >= 50 && @.price <= 500)] // price range
$.products[?(@.rating > 4.0 && @.inStock == true)] // high-rated and in stock
Use Case
Use comparison operators for data filtering scenarios like finding products within a price range, selecting users above a certain age, identifying records with specific status values, or implementing search and filter features in applications that consume JSON APIs.