MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / function_sum_example

Function function_sum_example

examples/src/jsonpath_examples.cpp:128–165  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

126}
127
128void function_sum_example()
129{
130 std::string data = R"(
131{
132
133"books":
134[
135 {
136 "title" : "A Wild Sheep Chase",
137 "author" : "Haruki Murakami",
138 "price" : 22.72
139 },
140 {
141 "title" : "The Night Watch",
142 "author" : "Sergei Lukyanenko",
143 "price" : 23.58
144 },
145 {
146 "title" : "The Comedians",
147 "author" : "Graham Greene",
148 "price" : 21.99
149 },
150 {
151 "title" : "The Night Watch",
152 "author" : "Phillips, David Atlee"
153 }
154]
155}
156 )";
157
158 auto j = jsoncons::json::parse(data);
159
160 // All titles whose price is greater than the average price
161 std::string expr = R"($.books[?(@.price > sum($.books[*].price)/length($.books[*].price))].title)";
162
163 jsoncons::json result = jsonpath::json_query(j, expr);
164 std::cout << result << "\n\n";
165}
166
167void function_avg_example()
168{

Callers 1

mainFunction · 0.85

Calls 2

parseFunction · 0.85
json_queryFunction · 0.85

Tested by

no test coverage detected