/** * Evaluate the JSON path and return the resulting value. * @param content the content to evaluate against * @return the result of the evaluation * @throws AssertionError if the evaluation fails */ @Nullable public Object evaluateJsonPath(String content) { try { return this.jsonPath.read(content); } catch (Throwable ex) { throw new AssertionError("No value at JSON path \"" + this.expression + "\"", ex); } }
@Override public String select(String text) { Object object = jsonPath.read(text); if (object == null) { return null; } if (object instanceof List) { List list = (List) object; if (list != null && list.size() > 0) { return toString(list.iterator().next()); } } return object.toString(); }
/** * Applies this JsonPath to the provided json file * * @param jsonFile file to read from * @param <T> expected return type * @return list of objects matched by the given path * @throws IOException */ @SuppressWarnings({"unchecked"}) public <T> T read(File jsonFile) throws IOException { return read(jsonFile, Configuration.defaultConfiguration()); }
@Override public Function<JsonNode, Object> makeJsonPathExtractor(final String expr) { final JsonPath jsonPath = JsonPath.compile(expr); return node -> valueConversionFunction(jsonPath.read(node, JSONPATH_CONFIGURATION)); }
@Override public Function<GenericRecord, Object> makeJsonPathExtractor(final String expr) { final JsonPath jsonPath = JsonPath.compile(expr); return record -> transformValue(jsonPath.read(record, JSONPATH_CONFIGURATION)); }
@Override public Function<Group, Object> makeJsonPathExtractor(String expr) { final JsonPath jsonPath = JsonPath.compile(expr); return record -> { Object val = jsonPath.read(record, jsonPathConfiguration); return finalizeConversion(val); }; }
@Test public void testIssue234() { Map<String, String> context = Maps.newHashMap(); context.put("key", "first"); Object value = JsonPath.read(context, "concat(\"/\", $.key)"); assertThat(value).isEqualTo("/first"); context.put("key", "second"); value = JsonPath.read(context, "concat(\"/\", $.key)"); assertThat(value).isEqualTo("/second"); } }
@Test(expected = PathNotFoundException.class) public void issue_11b() throws Exception { String json = "{ \"foo\" : [] }"; read(json, "$.foo[0].uri"); }
@Test public void read_store_book_wildcard() throws Exception { JsonPath path = JsonPath.compile("$.store.book[*]"); List<Object> list = path.read(DOCUMENT); Assertions.assertThat(list.size()).isEqualTo(4); }
@Test public void issue_42() { String json = "{" + " \"list\": [{" + " \"name\": \"My (String)\" " + " }] " + " }"; List<Map<String, String>> result = read(json, "$.list[?(@.name == 'My (String)')]"); assertThat(result).containsExactly(Collections.singletonMap("name", "My (String)")); }
@Test public void a_document_can_be_scanned_for_property_path() { List<String> result = read(DOCUMENT, "$..address.street"); assertThat(result).containsOnly("fleet street", "Baker street", "Svea gatan", "Söder gatan"); }
@Test public void issue_114_c() { String json = "{ \"p\": [\"valp\", \"valq\", \"valr\"] }"; List<String> result = read(json, "$.p[?(@[0] == 'valp')]"); assertThat(result).isEmpty(); }
@Test public void map_value_can_be_read_from_array() { List<String> result = JsonPath.read(SIMPLE_ARRAY, "$[*].foo"); assertThat(result).containsOnly("foo-val-0", "foo-val-1"); }
@Test public void get_from_index(){ List<Integer> result = JsonPath.read(JSON_ARRAY, "$[:3]"); assertThat(result, Matchers.contains(1,3,5)); }
@Test public void get_between_index_3(){ List<Integer> result = JsonPath.read(JSON_ARRAY, "$[0:2]"); assertThat(result, Matchers.contains(1,3)); }
@Test public void get_from_tail_index(){ List<Integer> result = JsonPath.read(JSON_ARRAY, "$[-3:]"); assertThat(result, Matchers.contains(8, 13, 20)); }
@Test public void get_indexes(){ List<Integer> result = JsonPath.read(JSON_ARRAY, "$[0,1,2]"); assertThat(result, Matchers.contains(1,3,5)); }
@Test public void a_filter_predicate_can_be_evaluated_on_decimal_criteria() { List<Map> result = JsonPath.read (ARRAY2, "$[?(@.decimal == 0.1)]"); assertThat(result).hasSize(1); assertThat(result.get(0)).contains(entry("decimal", 0.1)); }
@Test public void read_store_book_author() throws Exception { assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[*].author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.['store'].['book'][*].['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); assertThat(JsonPath.<List<String>>read(DOCUMENT, "$['store']['book'][*]['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); assertThat(JsonPath.<List<String>>read(DOCUMENT, "$['store'].book[*]['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); }