Skip to content

Commit 4e8e089

Browse files
committed
docs: clarify JSONPath filter expression syntax [skip ci]
1 parent a44ec9e commit 4e8e089

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python JSONPath Change Log
22

3-
## Version 0.10.2 (unreleased)
3+
## Version 0.10.2
44

55
**Fixes**
66

docs/advanced.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Arbitrary variables can be made available to [filter expressions](syntax.md#filters-expression) using the _filter_context_ argument to [`findall()`](quickstart.md#findallpath-data) and [`finditer()`](quickstart.md#finditerpath-data). _filter_context_ should be a [mapping](https://docs.python.org/3/library/typing.html#typing.Mapping) of strings to JSON-like objects, like lists, dictionaries, strings and integers.
66

7-
Filter context variables are selected using the _filter context selector_, which defaults to `_` and has usage similar to `$` and `@`.
7+
Filter context variables are selected using a filter query starting with the _filter context identifier_, which defaults to `_` and has usage similar to `$` and `@`.
88

99
```python
1010
import jsonpath

docs/syntax.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ $...title
122122

123123
### Filters (`[?EXPRESSION]`)
124124

125-
Filters allow you to remove nodes from a selection using a Boolean expression. When filtering a mapping-like object, `#` references the current key/property and `@` references the current value associated with `#`. When filtering a sequence-like object, `@` references the current item and `#` will hold the item's index in the sequence.
125+
Filters allow you to remove nodes from a selection using a Boolean expression. A _filter query_ is a JSONPath query nested within a filter expression. Every filter query must start with the root identifier (`$`), the current node identifier (`@`) or the [filter context](advanced.md#filter-variables) identifier (`_`).
126126

127127
```text
128128
$..products[?(@.price < $.price_cap)]
@@ -132,30 +132,36 @@ $..products[?(@.price < $.price_cap)]
132132
$..products[[email protected] < $.price_cap]
133133
```
134134

135+
When filtering a mapping-like object, `#` references the current key/property and `@` references the current value associated with `#`. When filtering a sequence-like object, `@` references the current item and `#` will hold the item's index in the sequence.
136+
135137
Comparison operators include `==`, `!=`, `<`, `>`, `<=` and `>=`. Plus `<>` as an alias for `!=`.
136138

137139
`in` and `contains` are membership operators. `left in right` is equivalent to `right contains left`.
138140

139-
`&&` and `||` are logical operators, `and` and `or` work too.
141+
`&&` and `||` are logical operators and terms can be grouped with parentheses. `and` and `or` work too.
140142

141143
`=~` matches the left value with a regular expression literal. Regular expressions use a syntax similar to that found in JavaScript, where the pattern to match is surrounded by slashes, optionally followed by flags.
142144

143145
```text
144146
$..products[?(@.description =~ /.*trainers/i)]
145147
```
146148

147-
Filter expressions can call predefined [function extensions](functions.md) too.
149+
A filter query on its own - one that is not part of a comparison expression - is an existence test. We also support comparing a filter query to the special `undefined` keyword. These two example are equivalent.
148150

149151
```text
150-
$.categories[?count(@.products.*) >= 2]
152+
$..products[[email protected]_price]
151153
```
152154

153-
`undefined` can be used to filter on the absence of a key/property or an undefined value returned from a filter function. `missing` is an alias for `undefined`.
154-
155155
```text
156156
$..products[[email protected]_price == undefined]
157157
```
158158

159+
Filter expressions can call predefined [function extensions](functions.md) too.
160+
161+
```text
162+
$.categories[?count(@.products.*) >= 2]
163+
```
164+
159165
### Union (`|`) and intersection (`&`)
160166

161167
Union (`|`) and intersection (`&`) are similar to Python's set operations, but we don't dedupe the matches (matches will often contain unhashable objects).

0 commit comments

Comments
 (0)