Returns a json array of values or normalized path expressions selected from a root json structure.
#include <jsoncons/jsonpath/json_query.hpp>
enum class result_type {value,path};
template<Json>
Json json_query(const Json& root,
const typename Json::string_view_type& path,
result_type result_t = result_type::value);| root | JSON value |
| path | JSONPath expression string |
| result_t | Indicates whether results are matching values (the default) or normalized path expressions |
Returns a json array containing either values or normalized path expressions matching the input path expression.
Returns an empty array if there is no match.
The examples below use the JSON text from Stefan Goessner's JsonPath (booklist.json).
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}Our first example returns all authors whose books are cheaper than $10.
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/json_query.hpp>
using namespace jsoncons;
using namespace jsoncons::jsonpath;
int main()
{
std::ifstream is("./input/booklist.json");
json booklist = json::parse(is);
json result = json_query(booklist,"$.store.book[?(@.price < 10)].author");
std::cout << pretty_print(result) << std::endl;
}Output:
["Nigel Rees","Herman Melville"]using namespace jsoncons;
using namespace jsoncons::jsonpath;
int main()
{
std::string path = "$.store.book[?(@.price < 10)].title";
json result = json_query(store,path,result_type::path);
std::cout << pretty_print(result) << std::endl;
}Output:
[
"$['store']['book'][0]['title']",
"$['store']['book'][2]['title']"
]