Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

How to add a new key:value pair to an existing json object #37

Closed
peterritter opened this issue Jun 5, 2015 · 5 comments
Closed

How to add a new key:value pair to an existing json object #37

peterritter opened this issue Jun 5, 2015 · 5 comments

Comments

@peterritter
Copy link

Hi
Thanks for this library! I do find the 'user guide' rather sparse however. I can't figure out how to add a key:value pair to a json object that already exists. Say I have :
JSON j = JSON::object{
{ "key1", "value1" },
{ "key2", false },
{ "key3", JSON::array { 1, 2, 3 } },
{ "key4", 666.333 },
{ "key5", JSON::array { 1, 2, JSON::object{ { "key1", "value1" },
{ "key6", false } } } }
};

Now I want to add another key:value pair like :
j.add("key", "value"); //no add() member!

How would I do this?

furthermore, how do I change an existing value?

j["key5"] = "something else";

Any way of doing this?

Many thanks, Peter

@skabbes
Copy link
Contributor

skabbes commented Jun 5, 2015

The Json interface is immutable (by design), however you can build up json objects by using Json::object (which is just a map<string, Json>underneath).

  • Json is immutable.
  • Json::array is actually vector<Json> and is therefore mutable
  • Json::object is actually map<string, Json> and is therefore mutable
Json::object mutable_json = Json::object {
    { "key1", "value1" },
    { "key2", false },
    { "key3", JSON::array { 1, 2, 3 } },
    { "key4", 666.333 }
};
mutable_json["key5"] = string {"something else"};

Json immutable_json = Json {mutable_json};

This makes sense (to me) because there is no guarantee that a Json is an object - what would it mean to do this?

Json value = 5;
value["new_key"] = 6; // doesn't compile

@peterritter
Copy link
Author

Hello skabbes

Many thanks for the response! The maintainer of the library may want to add your explanation to the user guide! I'm not really a JSON specialist, so I can't say what is 'legal' and what is not according to the json specification. However, from a purely practical perspective, I always think of the 'root' of the 'object' as a 'json object'. Would it really be 'Json' if it wasn't?

I am passing json objects around the network, as a quick way of putting messages together, and I needed to add an 'identifier token' to each message, and I just couldn't figure out how to do that. I did mange eventually to add an 'insert' function after struggling for quite a while.

Thanks

@maverick447
Copy link

hello skabbes,
Thanks for the tip! I am able to add a node but I need to add a Dictionary within a new entry into the map how would go ahead with it?

instead of this
mutable_json["key5"] = string {"something else"};
I need something like
mutable_json["key5"] = { add a name and value pair }

@skabbes
Copy link
Contributor

skabbes commented Dec 19, 2018

@maverick447 give this a try:

mutable_json["key5"] = Json::object {
  { "new_key", "new_value" }
};

@maverick447
Copy link

HI skabbes,
Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants