Search in sources :

Example 16 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by jayway.

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 17 with Filter

use of com.jayway.jsonpath.Filter in project JsonPath by jayway.

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)

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