Skip to content

Commit fe901dd

Browse files
committed
Improve JSON Pointer docs
1 parent 3ce253f commit fe901dd

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

docs/pointers.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,40 @@
22

33
**_New in version 0.8.0_**
44

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

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

9-
!!! note
9+
??? note "Extensions to RFC 6901"
1010

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+
```
1239

1340
## `resolve(data)`
1441

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ markdown_extensions:
6464
- pymdownx.inlinehilite
6565
- pymdownx.snippets
6666
- pymdownx.superfences
67+
- pymdownx.details
6768

6869
extra_css:
6970
- css/style.css

0 commit comments

Comments
 (0)