Skip to content

Like jsonpath::replace, except create? #304

Discussion options

You must be logged in to vote

Yes, the JSONPatch replace operation requires that the target location must exist for the operation to be successful, as specified in RFC 6902.

The jsonpointer extension includes functions add and replace that are consistent with the JSONPatch specification. I agree it would be useful to have alternative functions that will fill in the path. I'll look into adding that.

In the meantime, you can do it as follows:

#include <jsoncons/json.hpp>

using jsoncons::json;

void insert_path(json& j, const std::vector<std::string>& keys, const std::string& str)
{
    json* ptr = &j;

    for (const auto& item : keys)
    {
        auto r = ptr->try_emplace(item, json());
        ptr = &r.first->value(…

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
2 replies
@ganleynovetta
Comment options

@XNinety9
Comment options

Answer selected by ganleynovetta
Comment options

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

Comment options

You must be logged in to vote
3 replies
@danielaparker
Comment options

@XNinety9
Comment options

@danielaparker
Comment options

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