@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 = JSONUtils.filterJsonNodeWithPaths2(jsonNode, Arrays.asList("User/*/name", "RequestInfo" + "/api_id")); log.info(String.valueOf(newNode)); }
public 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; if(key.contains("*")) { newNode = objectMapper.createArrayNode(); ArrayNode arrayNode = (ArrayNode) jsonNode; for(JsonNode value : arrayNode) { ((ArrayNode) newNode).add(filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } } else { newNode = objectMapper.createObjectNode(); ObjectNode objectNode = (ObjectNode) jsonNode; JsonNode value = objectNode.get(key); ((ObjectNode) newNode).set(key, filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } return newNode; }
JSONUtils { public 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; if(key.contains("*")) { newNode = objectMapper.createArrayNode(); ArrayNode arrayNode = (ArrayNode) jsonNode; for(JsonNode value : arrayNode) { ((ArrayNode) newNode).add(filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } } else { newNode = objectMapper.createObjectNode(); ObjectNode objectNode = (ObjectNode) jsonNode; JsonNode value = objectNode.get(key); ((ObjectNode) newNode).set(key, filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } return newNode; } }
JSONUtils { public 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; if(key.contains("*")) { newNode = objectMapper.createArrayNode(); ArrayNode arrayNode = (ArrayNode) jsonNode; for(JsonNode value : arrayNode) { ((ArrayNode) newNode).add(filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } } else { newNode = objectMapper.createObjectNode(); ObjectNode objectNode = (ObjectNode) jsonNode; JsonNode value = objectNode.get(key); ((ObjectNode) newNode).set(key, filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } return newNode; }  }
JSONUtils { public 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; if(key.contains("*")) { newNode = objectMapper.createArrayNode(); ArrayNode arrayNode = (ArrayNode) jsonNode; for(JsonNode value : arrayNode) { ((ArrayNode) newNode).add(filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } } else { newNode = objectMapper.createObjectNode(); ObjectNode objectNode = (ObjectNode) jsonNode; JsonNode value = objectNode.get(key); ((ObjectNode) newNode).set(key, filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } return newNode; }  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 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; if(key.contains("*")) { newNode = objectMapper.createArrayNode(); ArrayNode arrayNode = (ArrayNode) jsonNode; for(JsonNode value : arrayNode) { ((ArrayNode) newNode).add(filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } } else { newNode = objectMapper.createObjectNode(); ObjectNode objectNode = (ObjectNode) jsonNode; JsonNode value = objectNode.get(key); ((ObjectNode) newNode).set(key, filterJsonNodeForPath(value, getRemainingJsonKeyForPath(filterPath))); } return newNode; }  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);  }