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

Function basics_json_example2

examples/src/basics_examples.cpp:136–164  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

134}
135
136void basics_json_example2()
137{
138 // Deserialize the booklist
139 std::ifstream is("./output/store.json");
140 json booklist;
141 is >> booklist;
142
143 // Use a JSONPath expression to find
144
145 // (1) The authors of books that cost less than $12
146 json result = json_query(booklist, "$[*][?(@.price < 12)].author");
147 std::cout << "(1) " << result << '\n';
148
149 // (2) The number of books
150 result = json_query(booklist, "$.length");
151 std::cout << "(2) " << result << '\n';
152
153 // (3) The third book
154 result = json_query(booklist, "$[2]");
155 std::cout << "(3) " << '\n' << pretty_print(result) << '\n';
156
157 // (4) The authors of books that were published in 2004
158 result = json_query(booklist, "$[*][?(@.date =~ /2004.*?/)].author");
159 std::cout << "(4) " << result << '\n';
160
161 // (5) The titles of all books that have ratings
162 result = json_query(booklist, "$[*][?(@.ratings)].title");
163 std::cout << "(5) " << result << '\n';
164}
165
166int main()
167{

Callers 1

mainFunction · 0.85

Calls 2

json_queryFunction · 0.85
pretty_printFunction · 0.85

Tested by

no test coverage detected