Merging Two JSON Structures Together

3,248 views
Skip to first unread message

Dave Ariens

unread,
Aug 4, 2015, 10:09:59 AM8/4/15
to jackson-user
I've posted a few topics regarding using mix-ins to restrict the object items that get serialized/de-serialized.  Everything is working rather well, however there's still a function that I'd like to bounce off this list in case there's a more best-practice method... 

Once I've received a restricted/limited attribute-set object within an API request I must load the existing object and set it's values for attributes/items in the updated object.  I currently perform this on the two serialized JSON values as it's easier than a recursive search through POJOs.  

Code below.  Is there a better way of handling this perhaps with functionality that's already included in Jackson that I may not be aware of?

public static JsonNode mergeJSON(JsonNode mainNode, JsonNode updateNode) {
Iterator<String> fieldNames = mainNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode jsonNode = mainNode.get(fieldName);
//if field exist and  json node is an array
if (jsonNode != null && jsonNode.isArray()) {
JsonNode tempArrayNode = updateNode.get(fieldName);
int count = 0;
//iterating array node
for (JsonNode node : jsonNode) {
mergeJSON(node, tempArrayNode.get(count));
count++;
}
} else {
// if field exists and is an embedded object
if (jsonNode != null && jsonNode.isObject()) {
mergeJSON(jsonNode, updateNode.get(fieldName));
} else {
if (mainNode instanceof ObjectNode) {
// Overwrite field
JsonNode value = updateNode.get(fieldName);
if (value != null) {
((ObjectNode) mainNode).replace(fieldName, value);
}
}
}
}
}
return mainNode;
}

Ian Barfield

unread,
Aug 4, 2015, 10:59:36 AM8/4/15
to jackson-user

There's nothing in jackson as far as I'm aware. I believe there may be an open issue about it. I have a very similar method in my own extension. I'll do a mental diff of them later to see if you may have missed any corner cases (or vice versa!). One suggestion though: I wouldn't capitalize JSON like that when referring to jackson objects. Mostly because there is another API that does that and for clarity's sake.


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tatu Saloranta

unread,
Aug 4, 2015, 1:03:28 PM8/4/15
to jackson-user
Correct, there is no "deep" merge functionality. There is an issue to support this, in some form, although focus there is on POJOs.
Separate merging functionality for JsonNode could very well make sense, and should be easier to implement. There are a few aspects to configuration (how to merge arrays, mostly; and what to do with scalar vs structure merges) that could be tricky, and/or app/use-case specific, but for the most part it should be quite simple.

There are JSON Patch implementations around, which is sort of related but not quite.

-+ Tatu +-

Reply all
Reply to author
Forward
0 new messages