You can also use the JsonPath project provided by REST Assured . This JsonPath project uses Groovy GPath expressions. In Maven, you can rely on this as follows:
<dependency> <groupId>com.jayway.restassured</groupId> <artifactId>json-path</artifactId> <version>2.4.0</version> </dependency>
<strong> Examples:
To get a list of all book categories:
List<String> categories = JsonPath.from(json).get("store.book.category");
Get the first category of books:
String category = JsonPath.from(json).get("store.book[0].category");
Get the latest category of books:
String category = JsonPath.from(json).get("store.book[-1].category");
Get all books priced from 5 to 15:
List<Map> books = JsonPath.from(json).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }");
GPath is very efficient, and you can use higher order functions and all Groovy data structures in your path expressions.
Johan source share