### JsonPath Examples Source: https://github.com/json-path/jsonpath/blob/master/README.md Examples of JsonPath expressions applied to a sample JSON structure. ```text $.store.book[*].author $..author $.store.* $.store..price $..book[2] $..book[-2] $..book[0,1] $..book[:2] $..book[1:2] $..book[-2:] $..book[2:] $..book[?(@.isbn)] $.store.book[?(@.price < 10)] $..book[?(@.price <= $['expensive'])] $..book[?(@.author =~ /.*REES/i)] $..* $..book.length() ``` -------------------------------- ### Implement Custom Cache Provider Source: https://github.com/json-path/jsonpath/blob/master/README.md Implement a custom cache provider by extending the Cache interface. This example shows a simple HashMap-based cache. ```java CacheProvider.setCache(new Cache() { //Not thread safe simple cache private Map map = new HashMap(); @Override public JsonPath get (String key){ return map.get(key); } @Override public void put (String key, JsonPath jsonPath){ map.put(key, jsonPath); } }); ``` -------------------------------- ### JsonPath Bracket Notation Example Source: https://github.com/json-path/jsonpath/blob/master/README.md Demonstrates the bracket notation for accessing elements, which can be an alternative to dot notation, especially for keys with special characters or spaces. ```jsonpath $['store']['book'][0]['title'] ``` -------------------------------- ### Implement Custom Predicate Source: https://github.com/json-path/jsonpath/blob/master/README.md Implement the Predicate interface to define custom filtering logic. This example checks if a book item contains an 'isbn' key. ```java Predicate booksWithISBN = new Predicate() { @Override public boolean apply(PredicateContext ctx) { return ctx.item(Map.class).containsKey("isbn"); } }; List> books = reader.read("$.store.book[?].isbn", List.class, booksWithISBN); ``` -------------------------------- ### JsonPath Dot Notation Example Source: https://github.com/json-path/jsonpath/blob/master/README.md Illustrates the dot notation for accessing elements within a JSON structure. The root is always represented by '$'. ```jsonpath $.store.book[0].title ``` -------------------------------- ### Combine Inline Predicates with AND/OR Source: https://github.com/json-path/jsonpath/blob/master/README.md Combine multiple conditions in inline predicates using '&&' for AND and '||' for OR. This example selects fiction books with a price less than 10, or reference books with a price greater than 10. ```java List> books = JsonPath.parse(json) .read("$.store.book[?(@.price < 10 && @.category == 'fiction')]"); ``` ```java List> books = JsonPath.parse(json) .read("$.store.book[?(@.category == 'reference' || @.price > 10)]"); ``` -------------------------------- ### Filter JSON using Filter API Source: https://github.com/json-path/jsonpath/blob/master/README.md Build complex filters using the Filter API with Criteria. This example selects fiction books with a price less than or equal to 10D. Note the '?' placeholder in the path. ```java import static com.jayway.jsonpath.JsonPath.parse; import static com.jayway.jsonpath.Criteria.where; import static com.jayway.jsonpath.Filter.filter; ... ... Filter cheapFictionFilter = filter( where("category").is("fiction").and("price").lte(10D) ); List> books = parse(json).read("$.store.book[?()]", cheapFictionFilter); ``` -------------------------------- ### Filter JSON by Price using Inline Predicate Source: https://github.com/json-path/jsonpath/blob/master/README.md Use inline predicates within the path to filter elements based on conditions. This example selects books with a price less than 10. ```java List> books = JsonPath.parse(json) .read("$.store.book[?(@.price < 10)]"); ``` -------------------------------- ### Combine Filter Criteria with OR and AND Source: https://github.com/json-path/jsonpath/blob/master/README.md Combine multiple criteria within a Filter using 'or' and 'and' methods. These examples demonstrate filtering for books where 'foo' exists OR 'bar' exists, and where 'foo' exists AND 'bar' exists. ```java Filter fooOrBar = filter( where("foo").exists(true)).or(where("bar").exists(true) ); Filter fooAndBar = filter( where("foo").exists(true)).and(where("bar").exists(true) ); ``` -------------------------------- ### Set Value in JSON Source: https://github.com/json-path/jsonpath/blob/master/README.md Use the 'set' method to modify a value at a specific path within the JSON. This example changes the author of the first book to 'Paul'. ```java String newJson = JsonPath.parse(json).set("$['store']['book'][0]['author']", "Paul").jsonString(); ``` -------------------------------- ### JsonPath Filter Expression Example Source: https://github.com/json-path/jsonpath/blob/master/README.md Shows how to use a filter expression `[?()]` to select elements from an array based on a condition. The `@` symbol refers to the current node. ```jsonpath [?(@.age > 18)] ``` -------------------------------- ### Negate Inline Predicate Source: https://github.com/json-path/jsonpath/blob/master/README.md Use the '!' operator to negate an inline predicate. This example selects books that do not meet the condition of being fiction with a price less than 10. ```java List> books = JsonPath.parse(json) .read("$.store.book[?(!(@.price < 10 && @.category == 'fiction'))]"); ``` -------------------------------- ### JsonPath Filter with String Comparison (Double Quotes) Source: https://github.com/json-path/jsonpath/blob/master/README.md Example of a filter expression comparing a string field to a value using double quotes for the string literal. ```jsonpath [?(@.color == "blue")] ``` -------------------------------- ### JsonPath Filter with String Comparison (Single Quotes) Source: https://github.com/json-path/jsonpath/blob/master/README.md Example of a filter expression comparing a string field to a value using single quotes for the string literal. ```jsonpath [?(@.color == 'blue')] ``` -------------------------------- ### Importing JsonPath Matchers Source: https://github.com/json-path/jsonpath/blob/master/json-path-assert/README.md Statically import the library's entry point for using JsonPath matchers in your assertions. ```java import static com.jayway.jsonpath.matchers.JsonPathMatchers.*; ``` -------------------------------- ### Configure Default JsonProvider Source: https://github.com/json-path/jsonpath/blob/master/README.md Set default JsonProvider and MappingProvider for JsonPath. This should only be done during application initialization. ```java Configuration.setDefaults(new Configuration.Defaults() { private final JsonProvider jsonProvider = new JacksonJsonProvider(); private final MappingProvider mappingProvider = new JacksonMappingProvider(); @Override public JsonProvider jsonProvider () { return jsonProvider; } @Override public MappingProvider mappingProvider () { return mappingProvider; } @Override public Set