Skip to content

Commit 833a6d6

Browse files
committed
More docs updates [skip ci]
1 parent a8bace4 commit 833a6d6

File tree

6 files changed

+60
-49
lines changed

6 files changed

+60
-49
lines changed

docs/advanced.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Filter Variables
44

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.
66

77
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

docs/async.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,3 @@ data = {
5959
best_a_team_players = jsonpath.findall_async("$.teams['A Team'][?rank >= 8]", data)
6060

6161
```
62-
63-
## Custom Async Item Getting
64-
65-
TODO:

docs/functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Filter Functions
22

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 built in filters. You can [define your own function extensions](advanced.md#function-extensions) too.
44

55
## `count()`
66

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ conda install -c conda-forge python-jsonpath
3434

3535
### Optional dependencies
3636

37-
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.
3838

3939
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).
4040

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.
4242

4343
## Example
4444

docs/pointers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ JSON Pointers are a fundamental component of JSON Patch ([RFC 6902](https://data
1010

1111
We have extended RFC 6901 to support:
1212

13-
- Interoperability with the JSONPath [keys selector](syntax.md#keys-or) (`~`)
13+
- Interoperability with the JSONPath [keys selector](syntax.md#keys-selector) (`~`)
1414
- A special non-standard syntax for targeting **keys or indices themselves**, used in conjunction with [Relative JSON Pointer](#torel)
1515

1616
**Keys Selector Compatibility**

docs/syntax.md

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# JSONPath Syntax
22

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.
46

57
## JSONPath Terminology
68

@@ -21,13 +23,13 @@ What follows is a description of these selectors, starting with the standard one
2123

2224
## Standard selectors and identifiers
2325

24-
### Root identifier (`$`)
26+
### Root identifier
2527

2628
The root identifier, `$`, refers to the outermost node in the target document. This can be an object, an array, or a scalar value.
2729

2830
A query containing only the root identifier simply returns the entire input document.
2931

30-
#### Example query
32+
**Example query**
3133

3234
```
3335
$
@@ -53,13 +55,13 @@ $
5355
]
5456
```
5557

56-
### Name selector (`.thing` or `['thing']`)
58+
### Name selector
5759

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"]`).
5961

6062
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.
6163

62-
#### Example query
64+
**Example query**
6365

6466
```text
6567
$.book.title
@@ -78,11 +80,11 @@ $.book.title
7880
["Moby Dick"]
7981
```
8082

81-
### Index selector (`[0]` or `[-1]`)
83+
### Index selector
8284

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.
8486

85-
#### Example query
87+
**Example query**
8688

8789
```text
8890
$.categories[0].name
@@ -101,11 +103,11 @@ $.categories[0].name
101103
["fiction"]
102104
```
103105

104-
### Wildcard selector (`.*` or `[*]`)
106+
### Wildcard selector
105107

106108
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).
107109

108-
#### Example query
110+
**Example query**
109111

110112
```text
111113
$.categories[*].name
@@ -124,11 +126,11 @@ $.categories[*].name
124126
["fiction", "non-fiction"]
125127
```
126128

127-
### Slice selector (`[start:end:step]`)
129+
### Slice selector
128130

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.
130132

131-
#### Example query
133+
**Example query**
132134

133135
```text
134136
$.items[1:4:2]
@@ -144,17 +146,17 @@ $.items[1:4:2]
144146
["b", "d"]
145147
```
146148

147-
### Filter selector (`[?expression]`)
149+
### Filter selector
148150

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.
150152

151153
When filtering a mapping-like object, `@` identifies the current member value. When filtering a sequence-like object, `@` identifies the current element.
152154

153155
Comparison operators include `==`, `!=`, `<`, `>`, `<=`, and `>=`. Logical operators `&&` (and) and `||` (or) can combine terms, and parentheses can be used to group expressions.
154156

155157
A filter expression on its own - without a comparison - is treated as an existence test.
156158

157-
#### Example query
159+
**Example query**
158160

159161
```text
160162
$..products[?(@.price < $.price_cap)]
@@ -182,13 +184,13 @@ Filter expressions can also call predefined [function extensions](functions.md).
182184

183185
## More on segments
184186

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.
186188

187189
### Segments with multiple selectors
188190

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.
190192

191-
#### Example query
193+
**Example query**
192194

193195
```text
194196
$.store.book[0,2]
@@ -213,11 +215,11 @@ $.store.book[0,2]
213215
]
214216
```
215217

216-
### Descendant segment (`..`)
218+
### Descendant segment
217219

218220
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.
219221

220-
#### Example query
222+
**Example query**
221223

222224
```text
223225
$..price
@@ -241,7 +243,13 @@ $..price
241243

242244
## Non-standard selectors and identifiers
243245

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.
245253

246254
### Key selector
247255

@@ -573,32 +581,39 @@ list-literal = "[" S literal *(S "," S literal) S "]"
573581
| `$.y[?'foo' in @.a]` | `{"a": ["foo", "bar"]}` | `$['y'][0]` | String literal in object |
574582
| `$.z[?(['bar', 'baz'] contains @.a)]` | `{"a": "bar"}` | `$['z'][1]` | List literal contains embedded query |
575583

576-
### The regex operator
584+
### Regex operator
577585

578-
TODO
586+
`=~` 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`.
579587

580-
### Union (`|`) and intersection (`&`)
588+
```
589+
$..products[?(@.description =~ /.*trainers/i)]
590+
```
581591

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
583593

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.
585595

586-
```text
587-
$..products.*.price | $.price_cap
588-
```
596+
The intersection operator, `&`, produces matches that are common to both left and right paths.
589597

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.
599+
600+
#### Syntax
591601

592-
```text
593-
$.categories[?(@.name == 'footwear')].products.* & $.categories[?(@.name == 'headwear')].products.*
594602
```
603+
jsonpath-query = root-identifier segments
595604
596-
Note that `|` and `&` are not allowed inside filter expressions.
605+
compound-jsonpath-query = jsonpath-query compound-op jsonpath-query
597606
598-
## Other differences
607+
compound-op = "|" /
608+
"&"
609+
```
599610

600-
This is a list of areas where Python JSONPath is more relaxed than [RFC 9535](https://datatracker.ietf.org/doc/html/rfc9535).
611+
#### Examples
601612

602-
- The root token (`$`) is optional and paths starting with a dot (`.`) are OK. `.thing` is the same as `$.thing`, as is `thing` and `$["thing"]`.
603-
- Leading and trailing whitespace is OK.
604-
- We support explicit comparisons to `undefined` (aka `missing`) as well as implicit existence tests.
613+
```text
614+
$..products.*.price | $.price_cap
615+
```
616+
617+
```text
618+
$.categories[?(@.name == 'footwear')].products.* & $.categories[?(@.name == 'headwear')].products.*
619+
```

0 commit comments

Comments
 (0)