Like jsonpath::replace, except create? #304
-
I have a vector K of strings. I would like to accomplish this: j[K[0]][K[1]]...[K[n]] = str I can't figure out for the life of me how to do this in jsoncons. It doesn't appear that jsonpath::replace works unless the path already exists, and all of my attempts to do it as a loop failed in some way that I can't parse without understanding the jsoncons code better than I do. Help please? I'm also trying jsonpatch, which seems like it might be the right solution, but no success so far. Every operation produces "error: JSON Patch add operation failed", but no indication as to why. It looks like maybe jsonpatch add only works one level deep or something? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
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();
}
*ptr = str;
}
int main()
{
std::vector<std::string> keys = {"foo","bar","baz"};
json j;
insert_path(j, keys, "str");
std::cout << j << "\n";
} Output: {"foo":{"bar":{"baz":"str"}}} |
Beta Was this translation helpful? Give feedback.
-
Just FYI, since 0.162.0, another option is #include <iostream>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
using jsoncons::json;
namespace jsonpointer = jsoncons::jsonpointer;
int main()
{
std::vector<std::string> keys = {"foo","bar","baz"};
jsonpointer::json_pointer location;
for (const auto& key : keys)
{
location /= key;
}
json j;
jsonpointer::add(j, location, "str", true); // create_if_missing set to true
std::cout << pretty_print(j) << "\n\n";
} Output: {"foo":{"bar":{"baz":"str"}}} |
Beta Was this translation helpful? Give feedback.
-
I have a very relatable issue. I have a map that looks like this: std::map<std::string, std::string> m {
{"$.title", "New item"}
{"$.content", "You got a new item"},
}; And I would like to transform it to a JSON like: {
"title": "New item",
"content": "You got a new item"
} |
Beta Was this translation helpful? Give feedback.
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: