com.github.hemantsonu20.json.JsonMerge Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-merge Show documentation
Show all versions of json-merge Show documentation
A light weight library to merge two json objects into a single json object.
package com.github.hemantsonu20.json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Iterator;
import java.util.Map;
/**
*
* This class provides methods to merge two json of any nested level into a single json.
*
*/
public class JsonMerge {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/**
*
* Method to merge two json objects into single json object.
*
* It merges two json of any nested level into a single json following below logic.
*
* - When keys are different, both keys with there values will be copied at same level.
* -
*
When keys are same at some level, following table denotes what value will be used.
*
*
*
* Src / Target
* JSON Value
* JSON Array
* JSON Object
*
*
*
*
* JSON Value1
* Src
* Src
* Src
*
*
* JSON Array
* Src2
* Merge
* Src
*
*
* JSON Object
* Src
* Src
* Merge3
*
*
*
*
* - 1 Json Value denotes boolean, number or string value in json.
* - 2 Src denotes
Src
value will be copied.
* - 3 Merge denotes both
Src
and Target
values will be merged.
*
*
*
*
* Examples
* Example 1
* Source Json
* {@code
* {
* "name": "json-merge-src"
* }
* }
* Target Json
* {@code
* {
* "name": "json-merge-target"
* }
* }
* Output
* {@code
* {
* "name": "json-merge-src"
* }
* }
* Example 2
* Source Json
* {@code
* {
* "level1": {
* "key1": "SrcValue1"
* }
* }
* }
* Target Json
* {@code
* {
* "level1": {
* "key1": "targetValue1",
* "level2": {
* "key2": "value2"
* }
* }
* }
* }
* Output
* {@code
* {
* "level1": {
* "key1": "SrcValue1",
* "level2": {
* "key2": "value2"
* }
* }
* }
* }
*
* @param srcJsonStr source json string
* @param targetJsonStr target json string
* @return merged json as a string
*/
public static String merge(String srcJsonStr, String targetJsonStr) {
try {
JsonNode srcNode = OBJECT_MAPPER.readTree(srcJsonStr);
JsonNode targetNode = OBJECT_MAPPER.readTree(targetJsonStr);
JsonNode result = merge(srcNode, targetNode);
return OBJECT_MAPPER.writeValueAsString(result);
} catch (JsonProcessingException e) {
throw new JsonMergeException("Unable to merge json", e);
}
}
public static JsonNode merge(JsonNode srcNode, JsonNode targetNode) {
// if both nodes are object node, merged object node is returned
if (srcNode.isObject() && targetNode.isObject()) {
return merge((ObjectNode) srcNode, (ObjectNode) targetNode);
}
// if both nodes are array node, merged array node is returned
if (srcNode.isArray() && targetNode.isArray()) {
return merge((ArrayNode) srcNode, (ArrayNode) targetNode);
}
// special case when src node is null
if (srcNode.isNull()) {
return targetNode;
}
return srcNode;
}
public static ObjectNode merge(ObjectNode srcNode, ObjectNode targetNode) {
ObjectNode result = OBJECT_MAPPER.createObjectNode();
Iterator> srcItr = srcNode.fields();
while (srcItr.hasNext()) {
Map.Entry entry = srcItr.next();
// check key in src json exists in target json or not at same level
if (targetNode.has(entry.getKey())) {
result.set(entry.getKey(), merge(entry.getValue(), targetNode.get(entry.getKey())));
} else {
// if key in src json doesn't exist in target json, just copy the same in result
result.set(entry.getKey(), entry.getValue());
}
}
// copy fields from target json into result which were missing in src json
Iterator> targetItr = targetNode.fields();
while (targetItr.hasNext()) {
Map.Entry entry = targetItr.next();
if (!result.has(entry.getKey())) {
result.set(entry.getKey(), entry.getValue());
}
}
return result;
}
public static ArrayNode merge(ArrayNode srcNode, ArrayNode targetNode) {
ArrayNode result = OBJECT_MAPPER.createArrayNode();
return result.addAll(srcNode).addAll(targetNode);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy