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;
}
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.