delete elements with jsonpath #379
-
Consider the following json: I would like to implement something like this auto elems = jsonpath::json_query(j1, "$.d.*", result_options::path);
auto j2 = Delete(j1, elems);
/* j2 should be {"c":{"a":1, "b":2}, "d":{}, "e": [1,2]} */ what's missing is |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
You could do it like this: #include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
using namespace jsoncons;
int main()
{
auto j = json::parse(R"({"c":{"a":1, "b":2}, "d":{"a":1, "b":2, "c":3}, "e": [1,2]})");
auto deleter = [](const json::string_view_type& path, json& val)
{if (val.is_object()) val.erase(val.object_range().begin(), val.object_range().end());};
jsonpath::json_replace(j, "$.d", deleter);
std::cout << j << std::endl;
} Output: {"c":{"a":1,"b":2},"d":{},"e":[1,2]} |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm trying to re-use this example and I am getting the below error, is this recent feature (do I need to update my header files)? 1>C:\src\WixJsonExtension\src\ca\jsoncons\json_type_traits.hpp(75,45): error C2338: static_assert failed: 'to_json not implemented' |
Beta Was this translation helpful? Give feedback.
-
yup looks like a version issue, updated to latest and it works now, thanks |
Beta Was this translation helpful? Give feedback.
You could do it like this:
Output: