|
2 | 2 |
|
3 | 3 | **_New in version 0.8.0_** |
4 | 4 |
|
5 | | -JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) is a string syntax for targeting a single value (JSON object, array or scalar) in a JSON document. Whereas a JSONPath has the potential to yield many values from a JSON document, a JSON Pointer can _resolve_ to at most one value. |
| 5 | +JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) is a string syntax for targeting a single value (JSON object, array, or scalar) within a JSON document. Unlike a JSONPath expression, which can yield multiple values, a JSON Pointer resolves to **at most one value**. |
6 | 6 |
|
7 | | -JSON Pointers are a fundamental part of JSON Patch ([RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902)). Each patch operation must have at least one pointer, identifying the target value. |
| 7 | +JSON Pointers are a fundamental component of JSON Patch ([RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902)), where each patch operation must have at least one pointer identifying the target location to modify. |
8 | 8 |
|
9 | | -!!! note |
| 9 | +??? note "Extensions to RFC 6901" |
10 | 10 |
|
11 | | - We have extended RFC 6901 to handle our non-standard JSONPath [keys selector](syntax.md#keys-or) and index/property pointers from [Relative JSON Pointer](#torel). |
| 11 | + We have extended RFC 6901 to support: |
| 12 | + |
| 13 | + - Interoperability with the JSONPath [keys selector](syntax.md#keys-or) (`~`) |
| 14 | + - A special non-standard syntax for targeting **keys or indices themselves**, used in conjunction with [Relative JSON Pointer](#torel) |
| 15 | + |
| 16 | + **Keys Selector Compatibility** |
| 17 | + |
| 18 | + The JSONPath **keys selector** (`.~` or `[~]`) allows expressions to target the *keys* of an object, rather than their associated values. To maintain compatibility when translating between JSONPath and JSON Pointer, our implementation includes special handling for this selector. |
| 19 | + |
| 20 | + While standard JSON Pointers always refer to values, we ensure that paths derived from expressions like `$.categories.~` can be represented in our pointer system. This is especially important when converting from JSONPath to JSON Pointer or when evaluating expressions that mix value and key access. |
| 21 | + |
| 22 | + **Key/Index Pointers (`#<key or index>`)** |
| 23 | + |
| 24 | + This non-standard pointer form represents **keys or indices themselves**, not the values they map to. Examples: |
| 25 | + |
| 26 | + - `#foo` points to the object key `"foo"` (not the value at `"foo"`) |
| 27 | + - `#0` points to the index `0` of an array (not the value at that index) |
| 28 | + |
| 29 | + This syntax is introduced to support the full capabilities of [Relative JSON Pointer](#torel), which allows references to both values and the *keys or indices* that identify them. To ensure that any `RelativeJSONPointer` can be losslessly converted into a `JSONPointer`, we use the `#<key or index>` form to represent these special cases. |
| 30 | + |
| 31 | + #### Example |
| 32 | + |
| 33 | + ```python |
| 34 | + from jsonpath import RelativeJSONPointer |
| 35 | + |
| 36 | + rjp = RelativeJSONPointer("1#") |
| 37 | + print(repr(rjp.to("/items/0/name"))) # JSONPointer('/items/#0') |
| 38 | + ``` |
12 | 39 |
|
13 | 40 | ## `resolve(data)` |
14 | 41 |
|
|
0 commit comments