1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
### jsoncons::jsonpath::jsonpath_expression::select
```cpp
Json select(const_reference root, result_options options = result_options()); (1) (since 0.172.0)
```
```cpp
template <typename BinaryOp>
void select(const_reference root, BinaryOp op,
result_options options = result_options()); (2) (since 0.172.0)
```
(1) Evaluates the root value against the compiled JSONPath expression and returns an array of values.
(2) Evaluates the root value against the compiled JSONPath expression and calls a provided
callback repeatedly with the results.
#### Parameters
<table>
<tr>
<td>root</td>
<td>Root JSON value</td>
</tr>
<tr>
<td><code>op</code></td>
<td>A function object that accepts a path and a reference to a Json value.
It must have function call signature equivalent to
<br/><br/><code>void fun(const basic_path_node<Json::char_type>& path, const Json& val);</code><br/><br/>
</tr>
<tr>
<td>result_options</td>
<td>Result options, a bitmask of type <a href="result_options.md">result_options</></td>
</tr>
</table>
### Examples
The examples below uses the sample data file `books.json`,
```json
{
"books":
[
{
"category": "fiction",
"title" : "A Wild Sheep Chase",
"author" : "Haruki Murakami",
"price" : 22.72
},
{
"category": "fiction",
"title" : "The Night Watch",
"author" : "Sergei Lukyanenko",
"price" : 23.58
},
{
"category": "fiction",
"title" : "The Comedians",
"author" : "Graham Greene",
"price" : 21.99
},
{
"category": "memoir",
"title" : "The Night Watch",
"author" : "Phillips, David Atlee"
}
]
}
```
#### Receive locations and values selected from a root value (since 0.172.0)
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
using json = jsoncons::json;
namespace jsonpath = jsoncons::jsonpath;
int main()
{
auto expr = jsoncons::jsonpath::make_expression<json>("$.books[*]");
std::ifstream is(/*path_to_books_file*/);
json doc = json::parse(is);
auto op = [&](const jsonpath::path_node& path, const json& value)
{
if (value.at("category") == "memoir" && !value.contains("price"))
{
std::cout << jsonpath::to_string(path) << ": " << value << "\n";
}
};
expr.select(root, op);
}
```
Output:
```
$['books'][3]: {"author":"Phillips, David Atlee","category":"memoir","title":"The Night Watch"}
```
|