You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/advanced.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## Filter Variables
4
4
5
-
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.
5
+
Arbitrary variables can be made available to [filter selectors](syntax.md#filter-selector) 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.
6
6
7
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 `@`.
Copy file name to clipboardExpand all lines: docs/functions.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Filter Functions
2
2
3
-
A filter function is a named function that can be called as part of a [filter selector](syntax.md#filters-expression) expression. Here we describe built-in filters. You can [define your own function extensions](advanced.md#function-extensions) too.
3
+
A filter function is a named function that can be called as part of a [filter selector](syntax.md#filter-selector). Here we describe builtin filters. You can [define your own function extensions](advanced.md#function-extensions) too.
By default, and without any additional dependencies, the JSONPath syntax supported by Python JSONPath is **very close** to RFC 9535. For strict compatibility with RFC 9535, install [regex](https://pypi.org/project/regex/) and [iregexp-check](https://pypi.org/project/iregexp-check/) packages too.
37
+
By default, and without any additional dependencies, the syntax supported by Python JSONPath is **very close** to RFC 9535. For strict compatibility with the specification, install [regex](https://pypi.org/project/regex/) and [iregexp-check](https://pypi.org/project/iregexp-check/) packages too.
38
38
39
39
With these two packages installed, the [`match()`](functions.md#match) and [`search()`](functions.md#search) filter functions will use [regex](https://pypi.org/project/regex/) instead of `re` from the standard library, and will validate regular expression patterns against [RFC 9485](https://datatracker.ietf.org/doc/html/rfc9485).
40
40
41
-
Aso see [strict mode](syntax.md#strict-mode) for more information about strict compatibility with RFC 9535.
41
+
See the [syntax guide](syntax.md) for more information about strict compatibility with RFC 9535 and extensions to the specification.
Copy file name to clipboardExpand all lines: docs/syntax.md
+55-40Lines changed: 55 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# JSONPath Syntax
2
2
3
-
By default, Python JSONPath extends the RFC 9535 specification with a few additional features and relaxed rules. If you need strict compliance with RFC 9535, you can enable strict mode, which enforces the standard without these extensions. In this guide, we first outline the standard syntax (see the specification for the formal definition), and then describe the non-standard extensions and their semantics in detail.
3
+
Python JSONPath extends the [RFC 9535](https://datatracker.ietf.org/doc/html/rfc9535) specification with additional features and relaxed rules. If you need strict compliance with RFC 9535, set `strict=True` when calling [`findall()`](convenience.md#jsonpath.findall), [`finditer()`](convenience.md#jsonpath.finditer), etc., which enforces the standard without these extensions.
4
+
5
+
In this guide, we first outline the standard syntax (see the specification for the formal definition), and then describe the non-standard extensions and their semantics in detail.
4
6
5
7
## JSONPath Terminology
6
8
@@ -21,13 +23,13 @@ What follows is a description of these selectors, starting with the standard one
21
23
22
24
## Standard selectors and identifiers
23
25
24
-
### Root identifier (`$`)
26
+
### Root identifier
25
27
26
28
The root identifier, `$`, refers to the outermost node in the target document. This can be an object, an array, or a scalar value.
27
29
28
30
A query containing only the root identifier simply returns the entire input document.
29
31
30
-
#### Example query
32
+
**Example query**
31
33
32
34
```
33
35
$
@@ -53,13 +55,13 @@ $
53
55
]
54
56
```
55
57
56
-
### Name selector (`.thing` or `['thing']`)
58
+
### Name selector
57
59
58
-
A _name selector_ matches the value of an object member by its key. You can write it in either **shorthand notation** (`.thing`) or **bracket notation** (`['thing']`).
60
+
A _name selector_ matches the value of an object member by its key. You can write it in either **shorthand notation** (`.thing`) or **bracket notation** (`['thing']` or `["thing"]`).
59
61
60
62
Dot notation can be used when the property name is a valid identifier. Bracket notation is required when the property name contains spaces, special characters, or starts with a number.
61
63
62
-
#### Example query
64
+
**Example query**
63
65
64
66
```text
65
67
$.book.title
@@ -78,11 +80,11 @@ $.book.title
78
80
["Moby Dick"]
79
81
```
80
82
81
-
### Index selector (`[0]` or `[-1]`)
83
+
### Index selector
82
84
83
-
Select an element from an array by its index. Indices are zero-based and enclosed in brackets. If the index is negative, items are selected from the end of the array.
85
+
The index selector selects an element from an array by its index. Indices are zero-based and enclosed in brackets, `[0]`. If the index is negative, items are selected from the end of the array.
84
86
85
-
#### Example query
87
+
**Example query**
86
88
87
89
```text
88
90
$.categories[0].name
@@ -101,11 +103,11 @@ $.categories[0].name
101
103
["fiction"]
102
104
```
103
105
104
-
### Wildcard selector (`.*` or `[*]`)
106
+
### Wildcard selector
105
107
106
108
The _wildcard selector_ matches all member values of an object or all elements in an array. It can be written as `.*` (shorthand notation) or `[*]` (bracket notation).
107
109
108
-
#### Example query
110
+
**Example query**
109
111
110
112
```text
111
113
$.categories[*].name
@@ -124,11 +126,11 @@ $.categories[*].name
124
126
["fiction", "non-fiction"]
125
127
```
126
128
127
-
### Slice selector (`[start:end:step]`)
129
+
### Slice selector
128
130
129
-
The slice selector allows you to select a range of elements from an array. You can specify a starting index, an ending index (exclusive), and an optional step to skip elements. Negative indices count from the end of the array, just like standard Python slicing.
131
+
The slice selector allows you to select a range of elements from an array. A start index, ending index and step size are all optional and separated by colons, `[start:end:step]`. Negative indices count from the end of the array, just like standard Python slicing.
130
132
131
-
#### Example query
133
+
**Example query**
132
134
133
135
```text
134
136
$.items[1:4:2]
@@ -144,17 +146,17 @@ $.items[1:4:2]
144
146
["b", "d"]
145
147
```
146
148
147
-
### Filter selector (`[?expression]`)
149
+
### Filter selector
148
150
149
-
Filters allow you to remove nodes from a selection based on a Boolean expression. A filter expression evaluates each node in the context of either the root (`$`) or the current node (`@`).
151
+
Filters allow you to remove nodes from a selection based on a Boolean expression, `[?expression]`. A filter expression evaluates each node in the context of either the root (`$`) or current (`@`) node.
150
152
151
153
When filtering a mapping-like object, `@` identifies the current member value. When filtering a sequence-like object, `@` identifies the current element.
152
154
153
155
Comparison operators include `==`, `!=`, `<`, `>`, `<=`, and `>=`. Logical operators `&&` (and) and `||` (or) can combine terms, and parentheses can be used to group expressions.
154
156
155
157
A filter expression on its own - without a comparison - is treated as an existence test.
156
158
157
-
#### Example query
159
+
**Example query**
158
160
159
161
```text
160
162
$..products[?(@.price < $.price_cap)]
@@ -182,13 +184,13 @@ Filter expressions can also call predefined [function extensions](functions.md).
182
184
183
185
## More on segments
184
186
185
-
So far we've seen shorthand notation and segments with just one selector. Here we cover the descendant segment and segments with multiple selectors.
187
+
So far we've seen shorthand notation (`.selector`) and segments with just one selector (`[selector]`). Here we cover the descendant segment and segments with multiple selectors.
186
188
187
189
### Segments with multiple selectors
188
190
189
-
A segment can include multiple selectors separated by commas and enclosed in square brackets (`[...]`). Any valid selector (names, indices, slices, filters, or wildcards) can appear in the list.
191
+
A segment can include multiple selectors separated by commas and enclosed in square brackets (`[selector, selector, ...]`). Any valid selector (names, indices, slices, filters, or wildcards) can appear in the list.
190
192
191
-
#### Example query
193
+
**Example query**
192
194
193
195
```text
194
196
$.store.book[0,2]
@@ -213,11 +215,11 @@ $.store.book[0,2]
213
215
]
214
216
```
215
217
216
-
### Descendant segment (`..`)
218
+
### Descendant segment
217
219
218
220
The descendant segment (`..`) visits all object member values and array elements under the current object or array, applying the selector or selectors that follow to each visited node. It must be followed by a shorthand selector (names, wildcards, etc.) or a bracketed list of one or more selectors.
219
221
220
-
#### Example query
222
+
**Example query**
221
223
222
224
```text
223
225
$..price
@@ -241,7 +243,13 @@ $..price
241
243
242
244
## Non-standard selectors and identifiers
243
245
244
-
The selectors and identifiers described in this section are an extension to RFC 9535. They are enabled by default. See [#strict-mode] for details on how to use JSONPath without these extensions.
246
+
The selectors and identifiers described in this section are an extension to the RFC 9535 specification. They are enabled by default. Set `strict=True` when constructing a [`JSONPathEnvironment`](api.md#jsonpath.JSONPathEnvironment), calling [`findall()`](convenience.md#jsonpath.findall), [`finditer()`](convenience.md#jsonpath.finditer), etc. to disable all non-standard features.
247
+
248
+
Also note that when `strict=False`:
249
+
250
+
- The root identifier (`$`) is optional and paths starting with a dot (`.`) are OK. `.thing` is the same as `$.thing`, as is `thing` and `$["thing"]`.
251
+
- Leading and trailing whitespace is OK.
252
+
- Explicit comparisons to `undefined` (aka `missing`) are supported as well as implicit existence tests.
245
253
246
254
### Key selector
247
255
@@ -573,32 +581,39 @@ list-literal = "[" S literal *(S "," S literal) S "]"
573
581
|`$.y[?'foo' in @.a]`|`{"a": ["foo", "bar"]}`|`$['y'][0]`| String literal in object |
`=~` is an infix operator that matches the left-hand side with a regular expression literal on the right-hand side. Regular expression literals use a syntax similar to that found in JavaScript, where the pattern to match is surrounded by slashes, `/pattern/`, optionally followed by flags, `/pattern/flags`.
579
587
580
-
### Union (`|`) and intersection (`&`)
588
+
```
589
+
$..products[?(@.description =~ /.*trainers/i)]
590
+
```
581
591
582
-
Union (`|`) and intersection (`&`) are similar to Python's set operations, but we don't dedupe the matches (matches will often contain unhashable objects).
592
+
### Union and intersection operators
583
593
584
-
The `|` operator combines matches from two or more paths. This example selects a single list of all prices, plus the price cap as the last element.
594
+
The union or concatenation operator, `|`, combines matches from two or more paths.
585
595
586
-
```text
587
-
$..products.*.price | $.price_cap
588
-
```
596
+
The intersection operator, `&`, produces matches that are common to both left and right paths.
589
597
590
-
The `&` operator produces matches that are common to both left and right paths. This example would select the list of products that are common to both the "footwear" and "headwear" categories.
598
+
Note that compound queries are not allowed inside filter expressions.
0 commit comments