Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/Add object fields #648

Closed
definename opened this issue Jul 6, 2017 · 2 comments
Closed

Update/Add object fields #648

definename opened this issue Jul 6, 2017 · 2 comments

Comments

@definename
Copy link

definename commented Jul 6, 2017

Hi,
Is it possible to modify array address without rewriting existing fields, for example I have this json:
nlohmann::json j = { { "name", "oleg" }, { "address", { "city", "kharkiv" } } };

And I need to add several fields to array address:
j["address"] = nlohmann::json { { "country", "ukraine"}, { "street", "svobody"} };
This variant rewrite array address and I lose data which were in it before I added new one.

So my question is: Is it possible to add data to existing array without loss existing one. Thanks.

@nlohmann
Copy link
Owner

nlohmann commented Jul 6, 2017

I think your example is more about JSON objects than JSON arrays. You can add elements like this:

j["address"]["email"] = "mail@example.com";

Alternatively, you can call insert on j["address"].

@definename
Copy link
Author

Thanks, it helps:

nlohmann::json jvalue = 
{
	{ "name", "oleg" },
	{ "address", { { "country", "ukraine" } } }
};

nlohmann::json data
{
	{ "street", "svobody" },
	{ "city", "kharkiv" }
};
jvalue["address"].insert(data.begin(), data.end());

@definename definename changed the title Update/Add array fields Update/Add object fields Jul 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants