|
| 1 | +# Query Iterators |
| 2 | + |
| 3 | +**_New in version 1.1.0_** |
| 4 | + |
| 5 | +In addition to [`findall()`](api.md#jsonpath.JSONPathEnvironment.findall) and [`finditer()`](api.md#jsonpath.JSONPathEnvironment.finditer), covered in the [quick start guide](./quickstart.md), Python JSONPath offers a fluent _query_ iterator interface. |
| 6 | + |
| 7 | +[`Query`](api.md#jsonpath.Query) objects provide chainable methods for manipulating a [`JSONPathMatch`](api.md#jsonpath.JSONPathMatch) iterator, just like you'd get from `finditer()`. Obtain a `Query` object using the package-level `query()` function or [`JSONPathEnvironment.query()`](api.md#jsonpath.JSONPathEnvironment.query). |
| 8 | + |
| 9 | +This example uses the query API to skip the first 5 matches, limit the total number of matches to 10, and get the value associated with each match. |
| 10 | + |
| 11 | +```python |
| 12 | +from jsonpath import query |
| 13 | + |
| 14 | +# data = ... |
| 15 | + |
| 16 | +values = ( |
| 17 | + query( "$.some[[email protected]]", data) |
| 18 | + .skip(5) |
| 19 | + .limit(10) |
| 20 | + .values() |
| 21 | +) |
| 22 | + |
| 23 | +for value in values: |
| 24 | + # ... |
| 25 | +``` |
| 26 | + |
| 27 | +## Chainable methods |
| 28 | + |
| 29 | +The following `Query` methods all return `self` (the same `Query` instance), so method calls can be chained to further manipulate the underlying iterator. |
| 30 | + |
| 31 | +| Method | Aliases | Description | |
| 32 | +| --------------- | ----------------------- | -------------------------------------------------- | |
| 33 | +| `skip(n: int)` | `drop` | Drop up to _n_ matches from the iterator. | |
| 34 | +| `limit(n: int)` | `head`, `take`, `first` | Yield at most _n_ matches from the iterator. | |
| 35 | +| `tail(n: int)` | `last` | Drop matches from the iterator up to the last _n_. | |
| 36 | + |
| 37 | +## Terminal methods |
| 38 | + |
| 39 | +These are terminal methods of the `Query` class. They can not be chained. |
| 40 | + |
| 41 | +| Method | Aliases | Description | |
| 42 | +| ------------- | ------- | ------------------------------------------------------------------------------------------- | |
| 43 | +| `values()` | | Return an iterable of objects, one for each match in the iterable. | |
| 44 | +| `locations()` | | Return an iterable of normalized paths, one for each match in the iterable. | |
| 45 | +| `items()` | | Return an iterable of (object, normalized path) tuples, one for each match in the iterable. | |
| 46 | +| `pointers()` | | Return an iterable of `JSONPointer` instances, one for each match in the iterable. | |
| 47 | +| `first_one()` | `one` | Return the first `JSONPathMatch`, or `None` if there were no matches. | |
| 48 | +| `last_one()` | | Return the last `JSONPathMatch`, or `None` if there were no matches. | |
| 49 | + |
| 50 | +## Tee |
| 51 | + |
| 52 | +And finally there's `tee()`, which creates multiple independent queries from one query iterator. It is not safe to use the initial `Query` instance after calling `tee()`. |
| 53 | + |
| 54 | +```python |
| 55 | +from jsonpath import query |
| 56 | + |
| 57 | +it1, it2 = query( "$.some[[email protected]]", data).tee() |
| 58 | + |
| 59 | +head = it1.head(10) # first 10 matches |
| 60 | +tail = it2.tail(10) # last 10 matches |
| 61 | +``` |
0 commit comments