3

I'm using nlohmann::json and all I need to do is copy a JSON object and then alter some of the keys in it. Is it possible to alter keys in nlohmann::json objects?

Essentially what I'm trying to do is the following:

json obj1 = {"key with space" : 10}
json obj2(obj1);
# .change_key not a real function
obj2.change_key("key with spaces", "key_with_spaces");

.change_key is the part that I need some help on.

Nick Chapman
  • 4,402
  • 1
  • 27
  • 41

1 Answers1

3

Probably the only way to do this is by adding and removing element:

json obj1 = {"key with space" : 10}
json obj2(obj1);

obj2["key with spaces"] = obj2.at("key_with_spaces");
obj2.erase("key_with_spaces");
bezet
  • 810
  • 7
  • 13