@Test public void filterJsonNodeForPathTest() throws IOException { JsonNode jsonNode = mapper.readTree("{\"RequestInfo\":{\"api_id\":\"1\",\"ver\":\"1\",\"ts\":null," + "\"action\":\"create\",\"did\":\"\",\"key\":\"\",\"msg_id\":\"\",\"requester_id\":\"\"," + "\"auth_token\":null},\"User\":[{\"userName\":\"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\"}]}"); JsonNode newNode = JacksonUtils.filterJsonNodeForPaths(jsonNode, Arrays.asList("User/*/name", "RequestInfo" + "/api_id", "asd/qwe")); JsonNode expectedNode = mapper.readTree("{\"RequestInfo\":{\"api_id\":\"1\"},\"User\":[{},{\"name\":\"ajay\"}]}"); assertEquals(expectedNode, newNode); }
static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath) { ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()); if(filterPath == null) return jsonNode; else if(jsonNode == null) return null; String key = getFirstJsonKeyForPath(filterPath); JsonNode newNode = null; try { if (key.contains("*")) { ArrayNode arrayNode = (ArrayNode) jsonNode; newNode = objectMapper.createArrayNode(); for (JsonNode value : arrayNode) { JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ArrayNode) newNode).add(filteredNode); } } else { ObjectNode objectNode = (ObjectNode) jsonNode; newNode = objectMapper.createObjectNode(); JsonNode value = objectNode.get(key); JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ObjectNode) newNode).set(key, filteredNode); } } catch (ClassCastException e) { log.error("Cannot find value for path : " + filterPath); } return newNode; }
JacksonUtils { static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath) { ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()); if(filterPath == null) return jsonNode; else if(jsonNode == null) return null; String key = getFirstJsonKeyForPath(filterPath); JsonNode newNode = null; try { if (key.contains("*")) { ArrayNode arrayNode = (ArrayNode) jsonNode; newNode = objectMapper.createArrayNode(); for (JsonNode value : arrayNode) { JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ArrayNode) newNode).add(filteredNode); } } else { ObjectNode objectNode = (ObjectNode) jsonNode; newNode = objectMapper.createObjectNode(); JsonNode value = objectNode.get(key); JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ObjectNode) newNode).set(key, filteredNode); } } catch (ClassCastException e) { log.error("Cannot find value for path : " + filterPath); } return newNode; } }
JacksonUtils { static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath) { ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()); if(filterPath == null) return jsonNode; else if(jsonNode == null) return null; String key = getFirstJsonKeyForPath(filterPath); JsonNode newNode = null; try { if (key.contains("*")) { ArrayNode arrayNode = (ArrayNode) jsonNode; newNode = objectMapper.createArrayNode(); for (JsonNode value : arrayNode) { JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ArrayNode) newNode).add(filteredNode); } } else { ObjectNode objectNode = (ObjectNode) jsonNode; newNode = objectMapper.createObjectNode(); JsonNode value = objectNode.get(key); JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ObjectNode) newNode).set(key, filteredNode); } } catch (ClassCastException e) { log.error("Cannot find value for path : " + filterPath); } return newNode; }  }
JacksonUtils { static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath) { ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()); if(filterPath == null) return jsonNode; else if(jsonNode == null) return null; String key = getFirstJsonKeyForPath(filterPath); JsonNode newNode = null; try { if (key.contains("*")) { ArrayNode arrayNode = (ArrayNode) jsonNode; newNode = objectMapper.createArrayNode(); for (JsonNode value : arrayNode) { JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ArrayNode) newNode).add(filteredNode); } } else { ObjectNode objectNode = (ObjectNode) jsonNode; newNode = objectMapper.createObjectNode(); JsonNode value = objectNode.get(key); JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ObjectNode) newNode).set(key, filteredNode); } } catch (ClassCastException e) { log.error("Cannot find value for path : " + filterPath); } return newNode; }  static JsonNode merge(JsonNode newNode, JsonNode originalNode); static JsonNode filterJsonNodeForPaths(JsonNode jsonNode, List<String> filterPaths); static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields); }
JacksonUtils { static JsonNode filterJsonNodeForPath(JsonNode jsonNode, String filterPath) { ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()); if(filterPath == null) return jsonNode; else if(jsonNode == null) return null; String key = getFirstJsonKeyForPath(filterPath); JsonNode newNode = null; try { if (key.contains("*")) { ArrayNode arrayNode = (ArrayNode) jsonNode; newNode = objectMapper.createArrayNode(); for (JsonNode value : arrayNode) { JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ArrayNode) newNode).add(filteredNode); } } else { ObjectNode objectNode = (ObjectNode) jsonNode; newNode = objectMapper.createObjectNode(); JsonNode value = objectNode.get(key); JsonNode filteredNode = filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath)); if(filteredNode != null && ! filteredNode.isNull()) ((ObjectNode) newNode).set(key, filteredNode); } } catch (ClassCastException e) { log.error("Cannot find value for path : " + filterPath); } return newNode; }  static JsonNode merge(JsonNode newNode, JsonNode originalNode); static JsonNode filterJsonNodeForPaths(JsonNode jsonNode, List<String> filterPaths); static JsonNode filterJsonNodeWithFields(JsonNode jsonNode, List<String> filterFields);  }