Refine search
public static void main(String[] args) { List<String> authors = JsonPath.read(json, "$.store.book[*].author"); System.out.println("authors: " + authors); // print ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] List<Map<String, Object>> expensiveBooks = JsonPath .using(Configuration.defaultConfiguration()) .parse(json) .read("$.store.book[?(@.price > 22)].title", List.class); System.out.println(expensiveBooks); // print ["Hello, Middle-earth! "] System.out.println(); String jsonHiWorld = "{\"message\":\"Hi\",\"place\":{\"name\":\"World!\"}}\""; String message = JsonPath.read(jsonHiWorld, "$.message"); String place = JsonPath.read(jsonHiWorld, "$.place.name"); System.out.println(message + " " + place); // print "Hi World!" }
/** * Variant of {@link #evaluateJsonPath(String)} with a target type. * This can be useful for matching numbers reliably for example coercing an * integer into a double. * @param content the content to evaluate against * @return the result of the evaluation * @throws AssertionError if the evaluation fails */ public Object evaluateJsonPath(String content, Class<?> targetType) { try { return JsonPath.parse(content).read(this.expression, targetType); } catch (Throwable ex) { String message = "No value at JSON path \"" + this.expression + "\""; throw new AssertionError(message, ex); } }
/** * @see JsonPath#isDefinite() */ public static boolean isPathDefinite(String path) { return compile(path).isDefinite(); }
@Test(expected = PathNotFoundException.class) public void missing_prop() { //Object read = JsonPath.using(Configuration.defaultConfiguration().setOptions(Option.THROW_ON_MISSING_PROPERTY)).parse(DOCUMENT).read("$.store.book[*].fooBar"); //Object read = JsonPath.using(Configuration.defaultConfiguration()).parse(DOCUMENT).read("$.store.book[*].fooBar"); Object read2 = JsonPath.using(Configuration.defaultConfiguration().addOptions(Option.REQUIRE_PROPERTIES)).parse(DOCUMENT).read("$.store.book[*].fooBar.not"); }
/** * 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); } }
@Test public void a_definite_path_can_be_returned_as_list() { Configuration conf = Configuration.builder().options(ALWAYS_RETURN_LIST).build(); assertThat((List)using(conf).parse("{\"foo\" : \"bar\"}").read("$.foo")).isInstanceOf(List.class); assertThat((List)using(conf).parse("{\"foo\": null}").read("$.foo")).isInstanceOf(List.class); assertThat((List)using(conf).parse("{\"foo\": [1, 4, 8]}").read("$.foo")).asList() .containsExactly(Arrays.asList(1, 4, 8)); }
/** * {@inheritDoc} */ public JsonAsserter assertNotDefined(String path) { try { Configuration c = Configuration.defaultConfiguration(); JsonPath.using(c).parse(jsonObject).read(path); throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path)); } catch (PathNotFoundException e) { } return this; }
/** * Applies this JsonPath to the provided json input stream * * @param jsonInputStream input stream 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(InputStream jsonInputStream) throws IOException { return read(jsonInputStream, Configuration.defaultConfiguration()); }
@Test(expected = PathNotFoundException.class) public void issue_22() throws Exception { Configuration configuration = Configuration.defaultConfiguration(); String json = "{\"a\":{\"b\":1,\"c\":2}}"; JsonPath.parse(json, configuration).read("a.d"); }
@Test public void issue_143() { String json = "{ \"foo\": { \"bar\" : \"val\" }, \"moo\": { \"cow\" : \"val\" } }"; Configuration configuration = Configuration.builder().options( Option.AS_PATH_LIST ).build(); List<String> pathList = JsonPath.using(configuration).parse(json).read(JsonPath.compile("$.*.bar")); assertThat(pathList).containsExactly("$['foo']['bar']"); }
@Test public void isbn_is_defaulted_when_option_is_provided() { List<String> result1 = JsonPath.using(JSON_SMART_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book.*.isbn"); assertThat(result1).containsExactly("0-553-21311-3","0-395-19395-8"); List<String> result2 = JsonPath.using(JSON_SMART_CONFIGURATION.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(JSON_DOCUMENT).read("$.store.book.*.isbn"); assertThat(result2).containsExactly(null, null, "0-553-21311-3", "0-395-19395-8"); } }
public JsonPathSelector(String jsonPathStr) { this.jsonPathStr = jsonPathStr; this.jsonPath = JsonPath.compile(this.jsonPathStr); }