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

can two json objects be concatenated? #1019

Closed
CheyenneForbes opened this issue Mar 20, 2018 · 8 comments
Closed

can two json objects be concatenated? #1019

CheyenneForbes opened this issue Mar 20, 2018 · 8 comments
Labels
kind: question solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@CheyenneForbes
Copy link

for example:

json j1 = {
 {obj1, value1},
 {obj2, value2}
};
json j2 = {
 {obj3, value3},
 {obj4, value4}
};

json j3 = j1 + j2;
@nlohmann
Copy link
Owner

Duplicate of #428, implemented with the update function.

@gregmarr
Copy link
Contributor

https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp#L14797-L14816

json j3 = j1;
j3.update(j2);

@nlohmann nlohmann added solution: duplicate the issue is a duplicate; refer to the linked issue instead kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 20, 2018
@p-i-
Copy link

p-i- commented Jan 31, 2019

Fantastic to find update.
Is there any particular reason not to overload + and +=?
That's the first thing I tried: j2 += j1
I think that would be a really neat syntax.

@jaredgrubb
Copy link
Contributor

There's not a lot to gain by adding another way to do this as there are only a few characters difference between the two. I personally like the Python mantra: "There should be one -- and preferably only one -- obvious way to do it.". C++ doesn't always live up to this, but it's a nice rule in general.

But, I'd also expect addition operations to have familiar properties like commutivity (eg, "j1+j2" and "j2+j1" should give the same result, but for JSON they wouldn't!). Sure, std::string has a non-commutative "+" but that doesn't mean we should give up on that goal.

@DolphinDream
Copy link

Does this update method work with merging recursively?
j1 = { “attributes”: { “background-color”: “red”}, “prop1”: 2 }
j2 = { “attributes”: { “text-color”: “blue”}, “prop2”: 3 }

Merging j1 and j2 (concatenating, updating) I would expect to get:

{ “attributes”: { “background-color”: “red”, “text-color”: “blue”}, “prop1”: 2, “prop2”: 3 }

I’m not sure if any changes to merge occurred since 3.3.0 .. but in this older version when merging only the top level properties are concatenated.. namely, if there is a duplicate to be merged (albeit with different subtree values) the previous property is being replaced by the duplicate property in the other j object. Essentially merging j1.update(j2) creates this:

{ “attributes”: { "text-color”: “blue”}, “prop1”: 2, “prop2”: 3 }

which removes the previous attributes values.

  fifo_json j1;
  j1["attributes"]["background-color"] = "red";
  j1["prop1"] = 2;
  cout << "j1 = " << endl;
  cout << j1.dump(2) << endl;

  fifo_json j2;
  j2["attributes"]["text-color"] = "blue";
  j2["prop2"] = 3;
  cout << "j2 = " << endl;
  cout << j2.dump(2) << endl;

  // concatenate
  j1.update(j2);
  cout << "j1+j2 = " << endl;
  cout << j1.dump(2) << endl;

gives the output:

j1 = 
{
  "attributes": {
    "background-color": "red"
  },
  "prop1": 2
}
j2 = 
{
  "attributes": {
    "text-color": "blue"
  },
  "prop2": 3
}
j1+j2 = 
{
  "attributes": {
    "text-color": "blue"
  },
  "prop1": 2,
  "prop2": 3
}

@gregmarr
Copy link
Contributor

gregmarr commented Sep 2, 2022

@DolphinDream There is now a recursive parameter to the update function. https://github.com/nlohmann/json/blob/master/docs/mkdocs/docs/api/basic_json/update.md

@DolphinDream
Copy link

@DolphinDream There is now a recursive parameter to the update function. https://github.com/nlohmann/json/blob/master/docs/mkdocs/docs/api/basic_json/update.md

Yeah.. i just noticed "Added merge_objects parameter in 3.10.4” .. which explains my 3.3.0 issue :)

@nlohmann
Copy link
Owner

nlohmann commented Sep 2, 2022

See https://json.nlohmann.me/api/basic_json/update/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: duplicate the issue is a duplicate; refer to the linked issue instead solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

6 participants