Skip to content

Commit 2cb7413

Browse files
committed
docs: mention JSON Pointer in README [skip ci]
1 parent 447f692 commit 2cb7413

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ A flexible JSONPath engine for Python.
2626

2727
- [Install](#install)
2828
- [Links](#links)
29-
- [Example](#example)
29+
- [Examples](#examples)
3030
- [License](#license)
3131

3232
## Install
@@ -52,7 +52,9 @@ pipenv install -u python-jsonpath
5252
- Source code: https://github.com/jg-rp/python-jsonpath
5353
- Issue tracker: https://github.com/jg-rp/python-jsonpath/issues
5454

55-
## Example
55+
## Examples
56+
57+
### JSONPath
5658

5759
```python
5860
import jsonpath
@@ -82,6 +84,41 @@ user_names = jsonpath.findall("$.users[[email protected] < 100].name", data)
8284
print(user_names) # ['John', 'Sally', 'Jane']
8385
```
8486

87+
### JSON Pointer
88+
89+
Since version 0.8.0, we also include an [RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901) compliant implementation of JSON Pointer.
90+
91+
```python
92+
from jsonpath import pointer
93+
94+
data = {
95+
"users": [
96+
{
97+
"name": "Sue",
98+
"score": 100,
99+
},
100+
{
101+
"name": "John",
102+
"score": 86,
103+
},
104+
{
105+
"name": "Sally",
106+
"score": 84,
107+
},
108+
{
109+
"name": "Jane",
110+
"score": 55,
111+
},
112+
]
113+
}
114+
115+
sue_score = pointer.resolve("/users/0/score", data)
116+
print(sue_score) # 100
117+
118+
jane_score = pointer.resolve(["users", 3, "score"], data)
119+
print(jane_score) # 55
120+
```
121+
85122
## License
86123

87124
`python-jsonpath` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.

‎docs/quickstart.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ other_users = path.findall(other_data)
138138
Resolve a [JSON Pointer](https://datatracker.ietf.org/doc/html/rfc6901). A JSON Pointer references a single object on a specific "path" in a JSON document. Here, _pointer_ can be a string representation of a JSON Pointer, or a list of parts that make up a pointer. _data_ can be a file-like object or string containing JSON formatted data, or equivalent Python objects.
139139

140140
```python
141-
import jsonpath
141+
from jsonpath import pointer
142142

143143
data = {
144144
"users": [
@@ -161,17 +161,17 @@ data = {
161161
]
162162
}
163163

164-
sue_score = jsonpath.resolve("/users/0/score", data)
164+
sue_score = pointer.resolve("/users/0/score", data)
165165
print(sue_score) # 100
166166

167-
jane_score = jsonpath.resolve(["users", 3, "score"], data)
167+
jane_score = pointer.resolve(["users", 3, "score"], data)
168168
print(jane_score) # 55
169169
```
170170

171171
If the pointer can't be resolved against the target JSON document - due to missing keys/properties or out of range indices - a `JSONPointerIndexError`, `JSONPointerKeyError` or `JSONPointerTypeError` will be raised, each of which inherit from `JSONPointerResolutionError`. A default value can be given, which will be returned in the event of a `JSONPointerResolutionError`.
172172

173173
```python
174-
import jsonpath
174+
from jsonpath import pointer
175175

176176
data = {
177177
"users": [
@@ -186,7 +186,7 @@ data = {
186186
]
187187
}
188188

189-
sue_score = jsonpath.resolve("/users/99/score", data, default=0)
189+
sue_score = pointer.resolve("/users/99/score", data, default=0)
190190
print(sue_score) # 0
191191
```
192192

0 commit comments

Comments
 (0)