Skip to content

delete elements with jsonpath #379

Answered by danielaparker
romange asked this question in Q&A
Discussion options

You must be logged in to vote

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]}

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by romange
Comment options

You must be logged in to vote
1 reply
@danielaparker
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants