@Test public void mergeWithGivenPaths() throws IOException { JsonNode originalNode = mapper.readTree("{\"RequestInfo\":{\"api_id\":\"1\",\"ver\":\"1\",\"ts\":null," + "\"action\":\"create\",\"did\":\"\",\"key\":\"\",\"msg_id\":\"\",\"requester_id\":\"\"," + "\"auth_token\":null},\"User\":[{\"userName\":\"ajay\",\"name\":\"ajay\",\"gender\":\"male\"," + "\"mobileNumber\":\"12312312\",\"active\":true,\"type\":\"CITIZEN\",\"password\":\"password\"}," + "{\"userName\":\"ajay\",\"name\":\"ajay\",\"gender\":\"male\",\"mobileNumber\":\"12312312\"," + "\"active\":true,\"type\":\"CITIZEN\",\"password\":\"password\"}]}"); List<String> filterPaths = Arrays.asList("$.RequestInfo.api_id", "$.asd", "$.User.[*].name"); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); JsonNode encryptedNode = mapper.readTree("[\"EncValue\", null, [\"EncryptedName1\",\"EncryptedName2\"]]"); JsonNode finalNode = JSONUtils.mergeNodesForGivenPaths(encryptedNode, originalNode, filterPaths); JsonNode expectedNode = mapper.readTree("{\"RequestInfo\":{\"api_id\":\"EncValue\",\"ver\":\"1\",\"ts\":null," + "\"action\":\"create\",\"did\":\"\",\"key\":\"\",\"msg_id\":\"\",\"requester_id\":\"\"," + "\"auth_token\":null},\"User\":[{\"userName\":\"ajay\",\"name\":\"EncryptedName1\"," + "\"gender\":\"male\",\"mobileNumber\":\"12312312\",\"active\":true,\"type\":\"CITIZEN\"," + "\"password\":\"password\"},{\"userName\":\"ajay\",\"name\":\"EncryptedName2\",\"gender\":\"male\"," + "\"mobileNumber\":\"12312312\",\"active\":true,\"type\":\"CITIZEN\",\"password\":\"password\"}]}"); assertEquals(expectedNode, finalNode); }
public static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths) throws IOException { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); JsonNode mergedNode = originalNode.deepCopy(); DocumentContext mergedNodeDocumentContext = JsonPath.using(configuration).parse(mergedNode.toString()); ArrayNode newArrayNode = (ArrayNode) newNode; for(int i = 0; i < filterPaths.size(); i++) { String path = filterPaths.get(i); if(mergedNodeDocumentContext.read(path) == null) continue; if(path.contains("*")) { ArrayNode values = (ArrayNode) newArrayNode.get(i); for(int j = 0; j < values.size(); j++) { String absolutePath = path.replace("*", String.valueOf(j)); mergedNodeDocumentContext.set(absolutePath, values.get(j).textValue()); } } else { mergedNodeDocumentContext.set(path, newArrayNode.get(i).textValue()); } } return mapper.readTree(mergedNodeDocumentContext.jsonString()); }
JSONUtils { public static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths) throws IOException { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); JsonNode mergedNode = originalNode.deepCopy(); DocumentContext mergedNodeDocumentContext = JsonPath.using(configuration).parse(mergedNode.toString()); ArrayNode newArrayNode = (ArrayNode) newNode; for(int i = 0; i < filterPaths.size(); i++) { String path = filterPaths.get(i); if(mergedNodeDocumentContext.read(path) == null) continue; if(path.contains("*")) { ArrayNode values = (ArrayNode) newArrayNode.get(i); for(int j = 0; j < values.size(); j++) { String absolutePath = path.replace("*", String.valueOf(j)); mergedNodeDocumentContext.set(absolutePath, values.get(j).textValue()); } } else { mergedNodeDocumentContext.set(path, newArrayNode.get(i).textValue()); } } return mapper.readTree(mergedNodeDocumentContext.jsonString()); } }
JSONUtils { public static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths) throws IOException { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); JsonNode mergedNode = originalNode.deepCopy(); DocumentContext mergedNodeDocumentContext = JsonPath.using(configuration).parse(mergedNode.toString()); ArrayNode newArrayNode = (ArrayNode) newNode; for(int i = 0; i < filterPaths.size(); i++) { String path = filterPaths.get(i); if(mergedNodeDocumentContext.read(path) == null) continue; if(path.contains("*")) { ArrayNode values = (ArrayNode) newArrayNode.get(i); for(int j = 0; j < values.size(); j++) { String absolutePath = path.replace("*", String.valueOf(j)); mergedNodeDocumentContext.set(absolutePath, values.get(j).textValue()); } } else { mergedNodeDocumentContext.set(path, newArrayNode.get(i).textValue()); } } return mapper.readTree(mergedNodeDocumentContext.jsonString()); }  }
JSONUtils { public static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths) throws IOException { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); JsonNode mergedNode = originalNode.deepCopy(); DocumentContext mergedNodeDocumentContext = JsonPath.using(configuration).parse(mergedNode.toString()); ArrayNode newArrayNode = (ArrayNode) newNode; for(int i = 0; i < filterPaths.size(); i++) { String path = filterPaths.get(i); if(mergedNodeDocumentContext.read(path) == null) continue; if(path.contains("*")) { ArrayNode values = (ArrayNode) newArrayNode.get(i); for(int j = 0; j < values.size(); j++) { String absolutePath = path.replace("*", String.valueOf(j)); mergedNodeDocumentContext.set(absolutePath, values.get(j).textValue()); } } else { mergedNodeDocumentContext.set(path, newArrayNode.get(i).textValue()); } } return mapper.readTree(mergedNodeDocumentContext.jsonString()); }  static JsonNode merge(JsonNode newNode, JsonNode originalNode); static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields); static JsonNode filterJsonNodeWithPaths2(JsonNode jsonNode, List<String> filterPaths); static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath); static ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths); static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths); }
JSONUtils { public static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths) throws IOException { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); JsonNode mergedNode = originalNode.deepCopy(); DocumentContext mergedNodeDocumentContext = JsonPath.using(configuration).parse(mergedNode.toString()); ArrayNode newArrayNode = (ArrayNode) newNode; for(int i = 0; i < filterPaths.size(); i++) { String path = filterPaths.get(i); if(mergedNodeDocumentContext.read(path) == null) continue; if(path.contains("*")) { ArrayNode values = (ArrayNode) newArrayNode.get(i); for(int j = 0; j < values.size(); j++) { String absolutePath = path.replace("*", String.valueOf(j)); mergedNodeDocumentContext.set(absolutePath, values.get(j).textValue()); } } else { mergedNodeDocumentContext.set(path, newArrayNode.get(i).textValue()); } } return mapper.readTree(mergedNodeDocumentContext.jsonString()); }  static JsonNode merge(JsonNode newNode, JsonNode originalNode); static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields); static JsonNode filterJsonNodeWithPaths2(JsonNode jsonNode, List<String> filterPaths); static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath); static ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths); static JsonNode mergeNodesForGivenPaths(JsonNode newNode, JsonNode originalNode, List<String> filterPaths);  }