Skip to content

Commit 6559f36

Browse files
committed
Docs
1 parent 82d3096 commit 6559f36

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎docs/api.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313

1414
::: jsonpath.JSONPointer
1515
handler: python
16+
17+
::: jsonpath.JSONPatch
18+
handler: python

‎docs/quickstart.md‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ with open("users.json") as fd:
5252

5353
## `finditer(path, data)`
5454

55-
Use [`jsonpath.finditer()`](api.md#jsonpath.env.JSONPathEnvironment.finditer) to create an iterator which yields instances of [`jsonpath.JSONPathMatch`](api.md#jsonpath.JSONPathMatch) for every object in some data that matches a JSONPath. It accepts the same arguments as [`findall()`](#findall), a path string and _data_ from which to select matches.
55+
Use [`jsonpath.finditer()`](api.md#jsonpath.env.JSONPathEnvironment.finditer) to iterate over instances of [`jsonpath.JSONPathMatch`](api.md#jsonpath.JSONPathMatch) for every object in _data_ that matches _path_. It accepts the same arguments as [`findall()`](#findall), a path string and data from which to select matches.
5656

5757
```python
5858
import jsonpath
@@ -169,11 +169,11 @@ if match:
169169
print(match.obj) # Sue
170170
```
171171

172-
## `resolve(pointer, data)`
172+
## `pointer.resolve(pointer, data)`
173173

174174
**_New in version 0.8.0_**
175175

176-
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.
176+
Resolve a JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) against some data. 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.
177177

178178
```python
179179
from jsonpath import pointer
@@ -230,6 +230,68 @@ print(sue_score) # 0
230230

231231
See also [`JSONPathMatch.pointer()`](api.md#jsonpath.match.JSONPathMatch.pointer), which builds a [`JSONPointer`](api.md#jsonpath.JSONPointer) from a `JSONPathMatch`.
232232

233+
## `patch.apply(patch, data)`
234+
235+
**_New in version 0.8.0_**
236+
237+
Apply a JSON Patch ([RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902)) to some data. A JSON Patch defines update operation to perform on a JSON document.
238+
239+
_patch_ can be a string or file-like object containing a valid JSON Patch document, or an iterable of dictionaries.
240+
241+
_data_ is the target JSON document to modify. It can be a file-like object or string containing JSON formatted data, or equivalent Python objects. **_data_ is modified in-place**.
242+
243+
```python
244+
from jsonpath import patch
245+
246+
patch_operations = [
247+
{"op": "add", "path": "/some/foo", "value": {"foo": {}}},
248+
{"op": "add", "path": "/some/foo", "value": {"bar": []}},
249+
{"op": "copy", "from": "/some/other", "path": "/some/foo/else"},
250+
{"op": "add", "path": "/some/foo/bar/-", "value": 1},
251+
]
252+
253+
data = {"some": {"other": "thing"}}
254+
patch.apply(patch_operations, data)
255+
print(data) # {'some': {'other': 'thing', 'foo': {'bar': [1], 'else': 'thing'}}}
256+
```
257+
258+
Use the [JSONPatch](api.md#jsonpath.JSONPatch) class to create a patch for repeated application.
259+
260+
```python
261+
from jsonpath import JSONPatch
262+
263+
patch = JSONPatch(
264+
[
265+
{"op": "add", "path": "/some/foo", "value": {"foo": {}}},
266+
{"op": "add", "path": "/some/foo", "value": {"bar": []}},
267+
{"op": "copy", "from": "/some/other", "path": "/some/foo/else"},
268+
{"op": "add", "path": "/some/foo/bar/-", "value": 1},
269+
]
270+
)
271+
272+
data = {"some": {"other": "thing"}}
273+
patch.apply(data)
274+
print(data) # {'some': {'other': 'thing', 'foo': {'bar': [1], 'else': 'thing'}}}
275+
```
276+
277+
[JSONPatch](api.md#jsonpath.JSONPatch) also offers a builder API for constructing JSON patch documents. We use strings as JSON Pointers in this example, but existing [JSONPointer](api.md#jsonpath.JSONPointer)s are OK too.
278+
279+
```python
280+
from jsonpath import JSONPatch
281+
282+
patch = (
283+
JSONPatch()
284+
.add("/some/foo", {"foo": []})
285+
.add("/some/foo", {"bar": []})
286+
.copy("/some/other", "/some/foo/else")
287+
.add("/some/foo/bar/-", "/some/foo/else")
288+
)
289+
290+
data = {"some": {"other": "thing"}}
291+
patch.apply(data)
292+
print(data) # {'some': {'other': 'thing', 'foo': {'bar': [1], 'else': 'thing'}}}
293+
```
294+
233295
## What's Next?
234296

235297
Read about user-defined filter functions at [Function Extensions](advanced.md#function-extensions), or see how to make extra data available to filters with [Extra Filter Context](advanced.md#extra-filter-context).

‎docs/syntax.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ This is a list of things that you might find in other JSONPath implementation th
181181
- We don't support extension functions of the form `selector.func()`.
182182
- We always return a list of matches from `jsonpath.findall()`, never a scalar value.
183183
- We do not support arithmetic in filter expression.
184+
- We don't allow dotted array indices. An array index must be surrounded by square brackets.
184185
- Python JSONPath is strictly read only. There are no update "selectors", but we do provide methods for converting `JSONPathMatch` instances to `JSONPointer`s, and a `JSONPatch` builder API for modifying JSON-like data structures using said pointers.
185186

186187
And this is a list of areas where we deviate from the [IETF JSONPath draft](https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-13).

0 commit comments

Comments
 (0)