Skip to content

Commit cea50cc

Browse files
committed
docs: add missing operators, in examples, and API reference
1 parent dbfb8a0 commit cea50cc

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ The JSONPath syntax in this project borrows from [JSONPath - XPath for JSON](htt
5353
| `?()` | applies a filter expression |
5454
| `/()` | applies a sorter expression |
5555
| `()` | applies a field-extractor expression |
56+
| `=~` | regex match operator (used in filter expressions) |
57+
| `in` | membership test operator (used in filter expressions) |
5658

5759
### Examples
5860

@@ -144,7 +146,21 @@ Support all python comparison operators (`==`, `!=`, `<`, `>`, `>=`, `<=`), pyth
144146
[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95, 'brand': {'version': 'v1.0.0'}}]
145147
```
146148

147-
`Note`: You must use double quote(`""`) instead of single quote(`''`) to wrap the compared string, because single quote(`''`) has another usage in this JSONPath syntax .
149+
The `in` operator can be used to check membership in lists or substrings in strings:
150+
151+
```python
152+
# Check if a value exists in a list
153+
>>> data = {"items": [{"tags": ["fruit", "red"]}, {"tags": ["vegetable"]}]}
154+
>>> JSONPath("$.items[?('fruit' in @.tags)]").parse(data)
155+
[{'tags': ['fruit', 'red']}]
156+
157+
# Check if a substring exists in a string
158+
>>> data = {"items": [{"name": "apple"}, {"name": "banana"}]}
159+
>>> JSONPath("$.items[?('app' in @.name)].name").parse(data)
160+
['apple']
161+
```
162+
163+
`Note`: You must use double quote(`""`) instead of single quote(`''`) to wrap the compared string, because single quote(`''`) has another usage in this JSONPath syntax.
148164

149165
#### Sorter Expression
150166

@@ -188,6 +204,32 @@ Update values in the JSON object using the `update` method.
188204
# Result: All book prices are multiplied by 0.9
189205
```
190206

207+
## API Reference
208+
209+
### Functions
210+
211+
- **`search(expr, data)`**: Search JSON data using a JSONPath expression. This function uses LRU caching internally for better performance when the same expression is used multiple times.
212+
213+
```python
214+
>>> from jsonpath import search
215+
>>> search("$..price", data)
216+
[8.95, 12.99, 8.99, 22.99, 19.95]
217+
```
218+
219+
- **`compile(expr)`**: Pre-compile a JSONPath expression for reuse. Equivalent to `JSONPath(expr)`.
220+
221+
```python
222+
>>> from jsonpath import compile
223+
>>> jp = compile("$.book[*].title")
224+
>>> jp.parse(data)
225+
['Sayings of the Century', 'Sword of Honour', 'Moby Dick', 'The Lord of the Rings']
226+
```
227+
228+
### Exceptions
229+
230+
- **`ExprSyntaxError`**: Raised when a JSONPath expression has invalid syntax (e.g., using sorter on non-collection types).
231+
- **`JSONPathTypeError`**: Raised when type-related errors occur (e.g., comparing incompatible types during sorting).
232+
191233
### Appendix: Example JSON data:
192234

193235
```python

0 commit comments

Comments
 (0)