Search in sources :

Example 1 with Filter

use of com.jayway.jsonpath.Filter in project tutorials by eugenp.

the class OperationIntegrationTest method givenJsonPathWithFilterPredicate_whenReading_thenCorrect.

@Test
public void givenJsonPathWithFilterPredicate_whenReading_thenCorrect() {
    Filter expensiveFilter = Filter.filter(Criteria.where("price").gt(20.00));
    List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?]", expensiveFilter);
    predicateUsageAssertionHelper(expensive);
}
Also used : Filter(com.jayway.jsonpath.Filter) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by json-path.

the class FilterTest method contains_filter_evaluates_on_array.

@Test
public void contains_filter_evaluates_on_array() {
    String json = "{\n" + "\"store\": {\n" + "    \"book\": [\n" + "        {\n" + "            \"category\": \"reference\",\n" + "            \"authors\" : [\n" + "                 {\n" + "                     \"firstName\" : \"Nigel\",\n" + "                     \"lastName\" :  \"Rees\"\n" + "                  }\n" + "            ],\n" + "            \"title\": \"Sayings of the Century\",\n" + "            \"price\": 8.95\n" + "        },\n" + "        {\n" + "            \"category\": \"fiction\",\n" + "            \"authors\": [\n" + "                 {\n" + "                     \"firstName\" : \"Evelyn\",\n" + "                     \"lastName\" :  \"Waugh\"\n" + "                  },\n" + "                 {\n" + "                     \"firstName\" : \"Another\",\n" + "                     \"lastName\" :  \"Author\"\n" + "                  }\n" + "            ],\n" + "            \"title\": \"Sword of Honour\",\n" + "            \"price\": 12.99\n" + "        }\n" + "    ]\n" + "  }\n" + "}";
    Filter filter = filter(where("authors[*].lastName").contains("Waugh"));
    List<String> result = JsonPath.parse(json).read("$.store.book[?].title", filter);
    Assertions.assertThat(result).containsExactly("Sword of Honour");
}
Also used : Filter(com.jayway.jsonpath.Filter) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 3 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by json-path.

the class FilterTest method combine_filter_deep_criteria.

@Test
public void combine_filter_deep_criteria() {
    String json = "[\n" + "   {\n" + "      \"first-name\" : \"John\",\n" + "      \"last-name\" : \"Irving\",\n" + "      \"address\" : {\"state\" : \"Texas\"}\n" + "   },\n" + "   {\n" + "      \"first-name\" : \"Jock\",\n" + "      \"last-name\" : \"Ewing\",\n" + "      \"address\" : {\"state\" : \"Texas\"}\n" + "   },\n" + "   {\n" + "      \"first-name\" : \"Jock\",\n" + "      \"last-name\" : \"Barnes\",\n" + "      \"address\" : {\"state\" : \"Nevada\"}\n" + "   } \n" + "]";
    Filter filter = filter(where("first-name").is("Jock").and("address.state").is("Texas"));
    List<Map<String, Object>> jocksInTexas1 = JsonPath.read(json, "$[?]", filter);
    List<Map<String, Object>> jocksInTexas2 = JsonPath.read(json, "$[?(@.first-name == 'Jock' && @.address.state == 'Texas')]");
    JsonPath.parse(json).json();
    assertThat((String) JsonPath.read(jocksInTexas1, "$[0].address.state"), is("Texas"));
    assertThat((String) JsonPath.read(jocksInTexas1, "$[0].first-name"), is("Jock"));
    assertThat((String) JsonPath.read(jocksInTexas1, "$[0].last-name"), is("Ewing"));
}
Also used : Filter(com.jayway.jsonpath.Filter) HashMap(java.util.HashMap) Map(java.util.Map) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 4 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by json-path.

the class IssuesTest method issue_129.

@Test
public void issue_129() throws Exception {
    final Map<String, Integer> match = new HashMap<String, Integer>();
    match.put("a", 1);
    match.put("b", 2);
    Map<String, Integer> noMatch = new HashMap<String, Integer>();
    noMatch.put("a", -1);
    noMatch.put("b", -2);
    Filter orig = filter(where("a").eq(1).and("b").eq(2));
    String filterAsString = orig.toString();
    Filter parsed = Filter.parse(filterAsString);
    assertThat(orig.apply(createPredicateContext(match))).isTrue();
    assertThat(parsed.apply(createPredicateContext(match))).isTrue();
    assertThat(orig.apply(createPredicateContext(noMatch))).isFalse();
    assertThat(parsed.apply(createPredicateContext(noMatch))).isFalse();
}
Also used : HashMap(java.util.HashMap) Filter(com.jayway.jsonpath.Filter) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 5 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by json-path.

the class JsonContextTest method cached_path_with_predicates.

@Test
public void cached_path_with_predicates() {
    Filter feq = Filter.filter(Criteria.where("category").eq("reference"));
    Filter fne = Filter.filter(Criteria.where("category").ne("reference"));
    DocumentContext JsonDoc = JsonPath.parse(JSON_DOCUMENT);
    List<String> eq = JsonDoc.read("$.store.book[?].category", feq);
    List<String> ne = JsonDoc.read("$.store.book[?].category", fne);
    Assertions.assertThat(eq).contains("reference");
    Assertions.assertThat(ne).doesNotContain("reference");
}
Also used : Filter(com.jayway.jsonpath.Filter) DocumentContext(com.jayway.jsonpath.DocumentContext) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Aggregations

Filter (com.jayway.jsonpath.Filter)17 Test (org.junit.Test)17 BaseTest (com.jayway.jsonpath.BaseTest)16 HashMap (java.util.HashMap)8 Map (java.util.Map)3 JsonObject (com.google.gson.JsonObject)2 DocumentContext (com.jayway.jsonpath.DocumentContext)2 Predicate (com.jayway.jsonpath.Predicate)2 Arrays.asList (java.util.Arrays.asList)2 List (java.util.List)2