@Test public void filterWithGivenPaths() 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 filteredNode = JSONUtils.filterJsonNodeWithPaths(originalNode, filterPaths); JsonNode expectednode = mapper.readTree("[\"1\", null, [\"ajay\",\"ajay\"]]"); assertEquals(expectednode, filteredNode); }
public static ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths) { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); DocumentContext jsonDocument = JsonPath.using(configuration).parse(jsonNode.toString()); ArrayNode filteredNode = mapper.createArrayNode(); for(String path : filterPaths) { Object value = jsonDocument.read(path); filteredNode.add(mapper.convertValue(value, JsonNode.class)); } return filteredNode; }
JSONUtils { public static ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths) { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); DocumentContext jsonDocument = JsonPath.using(configuration).parse(jsonNode.toString()); ArrayNode filteredNode = mapper.createArrayNode(); for(String path : filterPaths) { Object value = jsonDocument.read(path); filteredNode.add(mapper.convertValue(value, JsonNode.class)); } return filteredNode; } }
JSONUtils { public static ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths) { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); DocumentContext jsonDocument = JsonPath.using(configuration).parse(jsonNode.toString()); ArrayNode filteredNode = mapper.createArrayNode(); for(String path : filterPaths) { Object value = jsonDocument.read(path); filteredNode.add(mapper.convertValue(value, JsonNode.class)); } return filteredNode; }  }
JSONUtils { public static ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths) { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); DocumentContext jsonDocument = JsonPath.using(configuration).parse(jsonNode.toString()); ArrayNode filteredNode = mapper.createArrayNode(); for(String path : filterPaths) { Object value = jsonDocument.read(path); filteredNode.add(mapper.convertValue(value, JsonNode.class)); } return filteredNode; }  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 ArrayNode filterJsonNodeWithPaths(JsonNode jsonNode, List<String> filterPaths) { Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS); ObjectMapper mapper = new ObjectMapper(new JsonFactory()); DocumentContext jsonDocument = JsonPath.using(configuration).parse(jsonNode.toString()); ArrayNode filteredNode = mapper.createArrayNode(); for(String path : filterPaths) { Object value = jsonDocument.read(path); filteredNode.add(mapper.convertValue(value, JsonNode.class)); } return filteredNode; }  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);  }