|
1 | | -# Quick start |
| 1 | +# Quick Start |
2 | 2 |
|
3 | | -TODO |
| 3 | +This page gets you starting with Python JSONPath, see [JSONPath Syntax](syntax.md) for information on JSONPath selector syntax. |
| 4 | + |
| 5 | +## `findall` |
| 6 | + |
| 7 | +Find all objects matching a JSONPath with [`jsonpath.findall()`](api.md#jsonpath.env.JSONPathEnvironment.findall). It takes, as arguments, a JSONPath string and some _data_ object. It always returns a list of objects selected from the given data. |
| 8 | + |
| 9 | +_data_ can be a file-like object or string containing JSON formatted data, or a Python [`Mapping`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping) or [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence), like a dictionary or list. In this example we select user names from a dictionary containing a list of user dictionaries. |
| 10 | + |
| 11 | +```python |
| 12 | +import jsonpath |
| 13 | + |
| 14 | +data = { |
| 15 | + "users": [ |
| 16 | + { |
| 17 | + "name": "Sue", |
| 18 | + "score": 100, |
| 19 | + }, |
| 20 | + { |
| 21 | + "name": "John", |
| 22 | + "score": 86, |
| 23 | + }, |
| 24 | + { |
| 25 | + "name": "Sally", |
| 26 | + "score": 84, |
| 27 | + }, |
| 28 | + { |
| 29 | + "name": "Jane", |
| 30 | + "score": 55, |
| 31 | + }, |
| 32 | + ] |
| 33 | +} |
| 34 | + |
| 35 | +user_names = jsonpath.findall("$.users.*.name", data) |
| 36 | +``` |
| 37 | + |
| 38 | +Where `user_names` is now equal to: |
| 39 | + |
| 40 | +```json |
| 41 | +["Sue", "John", "Sally", "Jane"] |
| 42 | +``` |
| 43 | + |
| 44 | +If the same data were in a file called `users.json`, we might use `findall()` like this: |
| 45 | + |
| 46 | +```python |
| 47 | +import jsonpath |
| 48 | + |
| 49 | +with open("users.json") as fd: |
| 50 | + user_names = jsonpath.findall("$.users.*.name", fd) |
| 51 | +``` |
| 52 | + |
| 53 | +## `finditer` |
| 54 | + |
| 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. |
| 56 | + |
| 57 | +```python |
| 58 | +import jsonpath |
| 59 | + |
| 60 | +data = { |
| 61 | + "users": [ |
| 62 | + { |
| 63 | + "name": "Sue", |
| 64 | + "score": 100, |
| 65 | + }, |
| 66 | + { |
| 67 | + "name": "John", |
| 68 | + "score": 86, |
| 69 | + }, |
| 70 | + { |
| 71 | + "name": "Sally", |
| 72 | + "score": 84, |
| 73 | + }, |
| 74 | + { |
| 75 | + "name": "Jane", |
| 76 | + "score": 55, |
| 77 | + }, |
| 78 | + ] |
| 79 | +} |
| 80 | + |
| 81 | +matches = jsonpath.finditer("$.users.*.name", data) |
| 82 | +for match in matches: |
| 83 | + print(matches) |
| 84 | +``` |
| 85 | + |
| 86 | +The string representation of a [`JSONPathMatch`](api.md#jsonpath.JSONPathMatch) shows the matched object and the canonical path to that object in the given data. |
| 87 | + |
| 88 | +```text |
| 89 | +'Sue' @ $['users'][0]['name'] |
| 90 | +'John' @ $['users'][1]['name'] |
| 91 | +'Sally' @ $['users'][2]['name'] |
| 92 | +'Jane' @ $['users'][3]['name'] |
| 93 | +``` |
| 94 | + |
| 95 | +The selected object is available from a [`JSONPathMatch`](api.md#jsonpath.JSONPathMatch) as `obj` and its path, as a string, as `path`. Other useful properties of `JSONPathMatch` include a reference to the parent match, a list of child matches, and a `parts` tuple of keys and indices that make up the path. |
| 96 | + |
| 97 | +## `compile` |
| 98 | + |
| 99 | +When you have a JSONPath that needs to be matched against different data repeatedly, you can _compile_ the path ahead of time using [`jsonpath.compile()`](api.md#jsonpath.env.JSONPathEnvironment.compile). It takes a path as a string and returns a [`JSONPath`](api.md#jsonpath.JSONPath) instance. `JSONPath` has `findall()` and `finditer()` methods that behave similarly to package-level `findall()` and `finditer()`, just without the `path` argument. |
| 100 | + |
| 101 | +```python |
| 102 | +import jsonpath |
| 103 | + |
| 104 | +some_data = { |
| 105 | + "users": [ |
| 106 | + { |
| 107 | + "name": "Sue", |
| 108 | + "score": 100, |
| 109 | + }, |
| 110 | + { |
| 111 | + "name": "John", |
| 112 | + "score": 86, |
| 113 | + }, |
| 114 | + ] |
| 115 | +} |
| 116 | + |
| 117 | +other_data = { |
| 118 | + "users": [ |
| 119 | + { |
| 120 | + "name": "Sally", |
| 121 | + "score": 84, |
| 122 | + }, |
| 123 | + { |
| 124 | + "name": "Jane", |
| 125 | + "score": 55, |
| 126 | + }, |
| 127 | + ] |
| 128 | +} |
| 129 | + |
| 130 | +path = jsonpath.compile("$.users.*.name") |
| 131 | + |
| 132 | +some_users = path.findall(some_data) |
| 133 | +other_users = path.findall(other_data) |
| 134 | +``` |
| 135 | + |
| 136 | +## What's Next |
| 137 | + |
| 138 | +Read about user-defined filter functions at [Function Extensions](advanced.md), or see how to make extra data available to filters with [Extra Filter Context](advanced.md). |
| 139 | + |
| 140 | +`findall`, `finditer` and `compile` are shortcuts that use the default[`JSONPathEnvironment`](api.md#jsonpath.JSONPathEnvironment). `jsonpath.findall(path, data)` is equivalent to `jsonpath.JSONPathEnvironment().compile(path).findall(data)`. If you would like to use a custom environment, see [Advanced Usage](advanced.md). |
0 commit comments