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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
### jsonpath extension
The jsonpath extension implements [Stefan Goessner's JSONPath](http://goessner.net/articles/JsonPath/).
It provides functions for search and "search and replace" using JSONPath expressions.
### Classes
<table border="0">
<tr>
<td><a href="jsonpath_expression.md">jsonpath_expression</a></td>
<td>Represents the compiled form of a JSONPath string. (since 0.161.0)</td>
</tr>
<tr>
<td><a href="basic_json_location.md">basic_json_location</a></td>
<td>Represents the location of a specific value in a JSON document. (since 0.172.0)</td>
</tr>
<tr>
<td><a href="basic_path_node.md">basic_path_node</a></td>
<td>Represents a normalized path as a singly linked list where each node has a pointer to its (shared) parent node. (since 0.172.0)</td>
</tr>
</table>
### Functions
<table border="0">
<tr>
<td><a href="make_expression.md">make_expression</a></td>
<td>Returns a compiled JSONPath expression for later evaluation. (since 0.161.0)</td>
</tr>
<tr>
<td><a href="json_query.md">json_query</a></td>
<td>Searches for all values that match a JSONPath expression</td>
</tr>
<tr>
<td><a href="json_replace.md">json_replace</a></td>
<td>Search and replace using JSONPath expressions.</td>
</tr>
<tr>
<td><a href="flatten.md">flatten<br>unflatten</a></td>
<td>Flattens a json object or array.</td>
</tr>
</table>
### jsoncons JSONPath
[JSONPath](http://goessner.net/articles/JsonPath/) is a loosely standardized syntax for querying JSON.
There are many implementations and they differ in significant ways, see Christoph Burgmer's
[JSONPath comparison](https://cburgmer.github.io/json-path-comparison/). For details about the jsoncons
implementation, see the document [JsonCons JSONPath](https://danielaparker.github.io/JsonCons.Net/articles/JsonPath/JsonConsJsonPath.html).
In addition, the C++ implementation supports the following features:
- A length property on arrays and strings that returns the number of elements in an array, or the number of codepoints in a string,
e.g. `$[?(@.length == 2)]`.
- Custom functions, allowing the user to augment the list of built in JSONPath functions with user-provided functions (since 0.164.0).
### Examples
The examples use 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"
}
]
}
```
[Using json_query](#eg1)
[Using json_replace](#eg2)
[Using make_expression](#eg3)
[Custom functions](#eg4)
<div id="eg1"/>
#### Using json_query
```
#include <fstream>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
using json = jsoncons::json;
namespace jsonpath = jsoncons::jsonpath;
int main()
{
std::ifstream is(/*path_to_books_file*/);
json data = json::parse(is);
auto result1 = jsonpath::json_query(data, "$.books[1,1,3].title");
std::cout << "(1)\n" << pretty_print(result1) << "\n\n";
auto result2 = jsonpath::json_query(data, "$.books[1,1,3].title",
jsonpath::result_options::path);
std::cout << "(2)\n" << pretty_print(result2) << "\n\n";
//auto result3 = jsonpath::json_query(data, "$.books[1,1,3].title",
// jsonpath::result_options::value |
// jsonpath::result_options::nodups); (until 0.164.0)
auto result3 = jsonpath::json_query(data, "$.books[1,1,3].title",
jsonpath::result_options::nodups); (since 0.164.0)
std::cout << "(3)\n" << pretty_print(result3) << "\n\n";
//auto result4 = jsonpath::json_query(data, "$.books[1,1,3].title",
// jsonpath::result_options::nodups); (until 0.164.0)
auto result4 = jsonpath::json_query(data, "$.books[1,1,3].title",
jsonpath::result_options::nodups |
jsonpath::result_options::path); (since 0.164.0)
std::cout << "(4)\n" << pretty_print(result4) << "\n\n";
}
```
Output:
```
(1)
[
"The Night Watch",
"The Night Watch",
"The Night Watch"
]
(2)
[
"$['books'][1]['title']",
"$['books'][1]['title']",
"$['books'][3]['title']"
]
(3)
[
"The Night Watch",
"The Night Watch"
]
(4)
[
"$['books'][1]['title']",
"$['books'][3]['title']"
]
```
<div id="eg2"/>
#### Using json_replace
```
#include <fstream>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
using json = jsoncons::json;
namespace jsonpath = jsoncons::jsonpath;
int main()
{
std::ifstream is(/*path_to_books_file*/);
json data = json::parse(is);
auto f = [](const std::string& /*location*/, json& price)
{
price = std::round(price.as<double>() - 1.0);
};
jsonpath::json_replace(data, "$.books[*].price", f);
std::cout << pretty_print(data) << "\n";
}
```
Output:
```
{
"books": [
{
"author": "Haruki Murakami",
"category": "fiction",
"price": 22.0,
"title": "A Wild Sheep Chase"
},
{
"author": "Sergei Lukyanenko",
"category": "fiction",
"price": 23.0,
"title": "The Night Watch"
},
{
"author": "Graham Greene",
"category": "fiction",
"price": 21.0,
"title": "The Comedians"
},
{
"author": "Phillips, David Atlee",
"category": "memoir",
"title": "The Night Watch"
}
]
}
```
<div id="eg3"/>
#### Using make_expression
A [jsoncons::jsonpath::Using make_expression](Using make_expression.md)
represents the compiled form of a JSONPath expression. It allows you to
evaluate a single compiled expression on multiple JSON documents.
A `Using make_expression` is immutable and thread-safe.
```
#include <fstream>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
using json = jsoncons::json;
namespace jsonpath = jsoncons::jsonpath;
int main()
{
auto expr = jsonpath::make_expression<json>("$.books[1,1,3].title");
std::ifstream is(/*path_to_books_file*/);
json data = json::parse(is);
json result1 = expr.evaluate(data);
std::cout << "(1)\n" << pretty_print(result1) << "\n\n";
json result2 = expr.evaluate(data, jsonpath::result_options::path);
std::cout << "(2)\n" << pretty_print(result2) << "\n\n";
//json result3 = expr.evaluate(data, jsonpath::result_options::value |
// jsonpath::result_options::nodups); (until 0.164.0)
json result3 = expr.evaluate(data, jsonpath::result_options::nodups); (since 0.164.0)
std::cout << "(3)\n" << pretty_print(result3) << "\n\n";
//json result4 = expr.evaluate(data, jsonpath::result_options::nodups); (until 0.164.0)
json result4 = expr.evaluate(data, jsonpath::result_options::nodups |
jsonpath::result_options::path); (since 0.164.0)
std::cout << "(4)\n" << pretty_print(result4) << "\n\n";
}
```
Output:
```
(1)
[
"The Night Watch",
"The Night Watch",
"The Night Watch"
]
(2)
[
"$['books'][1]['title']",
"$['books'][1]['title']",
"$['books'][3]['title']"
]
(3)
[
"The Night Watch",
"The Night Watch"
]
(4)
[
"$['books'][1]['title']",
"$['books'][3]['title']"
]
```
<div id="eg4"/>
#### Custom functions
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
using json = jsoncons::json;
namespace jsonpath = jsoncons::jsonpath;
template <typename Json>
class my_custom_functions : public jsonpath::custom_functions<Json>
{
public:
my_custom_functions()
{
this->register_function("divide", // function name
2, // number of arguments
[](jsoncons::span<const jsonpath::parameter<Json>> params,
std::error_code& ec) -> Json
{
const Json& arg0 = params[0].value();
const Json& arg1 = params[1].value();
if (!(arg0.is_number() && arg1.is_number()))
{
ec = jsonpath::jsonpath_errc::invalid_type;
return Json::null();
}
return Json(arg0.as<double>() / arg1.as<double>());
}
);
}
};
int main()
{
json root = json::parse(R"([{"foo": 60, "bar": 10},{"foo": 60, "bar": 5}])");
json result = jsonpath::json_query(root,
"$[?(divide(@.foo, @.bar) == 6)]",
jsonpath::result_options(),
my_custom_functions<json>()); // (since 0.164.0)
std::cout << pretty_print(result) << "\n\n";
}
```
Output:
```
[{"bar": 10,"foo": 60}]
```
|