JSONPath Exists Check — Filter by Property Existence
Use JSONPath exists checks to filter elements based on whether a property is present. Learn the [?(@.key)] syntax for optional field detection.
Detailed Explanation
Exists Check in JSONPath
An exists check filters array elements based on whether a specific property is present, regardless of its value. Simply referencing a property inside [?()] without a comparison operator performs an existence test.
Syntax
$.array[?(@.property)] // elements where property exists
$.array[?(!@.property)] // elements where property does NOT exist (some implementations)
How It Works
Given:
{
"items": [
{ "name": "Widget", "price": 9.99, "discount": 0.10 },
{ "name": "Gadget", "price": 24.99 },
{ "name": "Doohickey", "price": 4.99, "discount": 0.05 },
{ "name": "Thingamajig", "price": 14.99 }
]
}
$.items[?(@.discount)]returns Widget and Doohickey — the items that have adiscountproperty.$.items[?(@.discount)].namereturns["Widget", "Doohickey"].
What Counts as "Exists"?
The exists check verifies that the property is defined on the object. Important nuances:
| Value | Exists? | Notes |
|---|---|---|
"hello" |
Yes | Non-empty string |
0 |
Yes | Zero is a valid value |
false |
Yes* | Boolean false — behavior varies by implementation |
null |
Yes* | Null means defined but empty — varies by implementation |
| (missing) | No | Property not in the object |
Some implementations treat null and false as "not existing" because they are falsy. Always test with your specific JSONPath library.
Practical Patterns
- Find incomplete records:
$.users[?(!@.email)]— users without email addresses. - Find enriched data:
$.products[?(@.metadata)]— products that have metadata attached. - Conditional processing: check for optional fields before accessing them.
Combining Exists with Other Conditions
$.items[?(@.discount && @.price > 10)]
This selects items that both have a discount property AND a price greater than 10.
Use Case
Use exists checks when working with JSON data that has optional fields — for example, finding API responses that include an error field, filtering records that have been enriched with additional metadata, or identifying incomplete entries in a dataset.