Skip to content

Commit 1c15fab

Browse files
committed
Add usage documentation to README
1 parent 0c329db commit 1c15fab

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

README.md

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ See also [Python JSONPath](https://github.com/jg-rp/python-jsonpath), which also
1010

1111
- [Install](#install)
1212
- [Links](#links)
13-
- [Examples](#examples)
13+
- [Usage](#usage)
1414
- [License](#license)
1515

1616
## Install
@@ -24,9 +24,58 @@ TODO:
2424
- Source code: https://github.com/jg-rp/python-jsonpath-rfc9535
2525
- Issue tracker: https://github.com/jg-rp/python-jsonpath-rfc9535/issues
2626

27-
## Examples
27+
## Usage
2828

29-
TODO:
29+
NOTE: If you're coming from [Python JSONPath](https://github.com/jg-rp/python-jsonpath), this API is similar but different. Neither implementation is guaranteed to be a drop-in replacement for the other.
30+
31+
### `query(path, data)`
32+
33+
Apply JSONPath expression _path_ to _data_. _data_ should arbitrary, possible nested, Python dictionaries, lists, strings, integers, floats, booleans or `None`, as you would get from [`json.load()`](https://docs.python.org/3/library/json.html#json.load).
34+
35+
A list of `JSONPathNode` instances is returned, one for each value in _data_ matched by _path_. The returned list will be empty if there were no matches.
36+
37+
Each `JSONPathNode` has:
38+
39+
- a `value` property, which is the JSON-like value associated with the node.
40+
- a `parts` property, which is a tuple of property names and array/list indices that were required to reach the node's value in the target JSON document.
41+
- a `path()` method, which returns the normalized path to the node in the target JSON document.
42+
43+
The returned list is a subclass of `list` with some helper methods.
44+
45+
- `values()` returns a list of values, one for each node.
46+
- `values_or_singular()` returns a scalar value if the list has exactly one node, or a list of values otherwise.
47+
48+
**Example:**
49+
50+
```python
51+
from jsonpath_rfc9535 import query
52+
53+
data = {
54+
"users": [
55+
{"name": "Sue", "score": 100},
56+
{"name": "John", "score": 86, "admin": True},
57+
{"name": "Sally", "score": 84, "admin": False},
58+
{"name": "Jane", "score": 55},
59+
],
60+
"moderator": "John",
61+
}
62+
63+
for node in query("$.users[?@.score > 85]", data):
64+
print(f"{node.value} at '{node.path()}'")
65+
66+
# {'name': 'Sue', 'score': 100} at '$['users'][0]'
67+
# {'name': 'John', 'score': 86, 'admin': True} at '$['users'][1]'
68+
```
69+
70+
### findall(path, data)
71+
72+
`findall()` accepts the same arguments as [`query()`](#querypath-data), but returns a list of value rather than a list of nodes.
73+
74+
`findall()` is equivalent to `query("$.some.thing", data).values()`.
75+
76+
### finditer(path, data)
77+
78+
`finditer()` accepts the same arguments as [`query()`](#querypath-data), but returns an iterator over `JSONPathNode` instances rather than a list. This could be useful if you're expecting a large number of results that you don't want to load into memory all at once.
3079

3180
## License
3281

0 commit comments

Comments
 (0)