The jsonpointer extension implements the IETF standard JavaScript Object Notation (JSON) Pointer
contains | Returns `true` if the json document contains the given json pointer |
get | Get a value from a JSON document using Json Pointer path notation. |
insert | Inserts a value in a JSON document using Json Pointer path notation, if the path doesn't specify an object member that already has the same key. |
insert_or_assign | Inserts a value in a JSON document using Json Pointer path notation, or if the path specifies an object member that already has the same key, assigns the new value to that member. |
remove | Removes a value from a JSON document using Json Pointer path notation. |
replace | Replaces a value in a JSON document using Json Pointer path notation. |
Example. Select author from second book
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
using namespace jsoncons;
int main()
{
json doc = json::parse(R"(
[
{ "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
}
]
)");
// Using exceptions to report errors
try
{
json result = jsonpointer::get(doc, "/1/author");
std::cout << "(1) " << result << std::endl;
}
catch (const jsonpointer::jsonpointer_error& e)
{
std::cout << e.what() << std::endl;
}
// Using error codes to report errors
std::error_code ec;
json result = jsonpointer::get(doc, "/0/title", ec);
if (ec)
{
std::cout << ec.message() << std::endl;
}
else
{
std::cout << "(2) " << result << std::endl;
}
}
Output:
(1) "Evelyn Waugh"
(2) "Sayings of the Century"
jsonpointer::get may also be used to query the nested data items of a packed CBOR value.