0

I want to merge two ObjectNode together, but with the best complexity. I aware about one way, I can use setAll method, but it returns JsonNode, therefore I must convert it. The best way that I found for the conversion procedure was How to convert JsonNode to ObjectNode. I think this conversion once iterate over Json and maybe we can find a better solution.

The second way that I think is iterate over the second ObjectNode and add one by one to first ObjectNode with put method But both of these solutions have cost, what is the best way to merge?

SpongeBob
  • 383
  • 3
  • 16

1 Answers1

0

I think I find the best way. Based on How to modify JsonNode in Java? (Casting JsonNode to ObjectNode) it seems we can easily cast JsonNode to ObjectNode thus I think below code will work(I didn't test yet): consider we have two JSONs(JsonNode):

jsonNode json1;//initilized
jsonNode json2;//initilized
JsonNode mergedJson = ((JsonObject)json2).setAll(json1);

and if we want to treat mergedJson as JsonObject, again we can merge and do

SpongeBob
  • 383
  • 3
  • 16