How to use GPath Groovy in Rest-Assured

You may search this topic over the internet but not quite sure “How many methods, symbols you can use in the query and which are they?“. This post is for you, it provides a logical approach to help you investigate by yourself.

Start with a very simple json:

    "firstName": "John",
    "lastName": "doe",
    "age": 26,
    "address": {
        "streetAddress": "naist street",
        "city": "Nara",
        "postalCode": "630-0192"
    "phoneNumbers": [
            "type": "iPhone",
            "number": "0123-4567-8888"
            "type": "home",
            "number": "0123-4567-8910"

To get data from json, the syntax of Rest-Assured is:

  • JsonPath.from(response) –> create JsonPath object from String.
  • Parameter for get() method will be the query. In this case, it’s address.postalCode

I. Rules

First, you have to know how groovy converts the json to object:

  • {} in json –> a Map
  • [] in json –> a List

Second, you can use all the List’s methods if you’re dealing with a json array [], and you can use all the Map’s methods for a json object {}.

Map and List in Groovy are inherited from Java, then you can use both set of methods.

II. Map’s Methods

  • Get all keys of the json
//method keySet() from Map java
Set<?> keys = JsonPath.from(response).get("keySet()");
//[firstName, lastName, age, address, phoneNumbers]
  • Check contains key “firstName”
//method containsKey(Object key) from Map java
boolean check = JsonPath.from(response).get("containsKey('firstName')");
  • Check size of the Map
//method size() from Map java
int size = JsonPath.from(response).get("size()");
  • How many key-value pair that having value=26.
//method count(Closure) from Map groovy
int value = JsonPath.from(response).get("count{k,v -> v==26}");
  • Sort the Map by key
//method toSorted() from Map groovy
Map<String, ?> newJson = JsonPath.from(response).get("toSorted()");
newJson.forEach((k,v) -> System.out.println(k +":" +v));

//address:{streetAddress=naist street, city=Nara, postalCode=630-0192}
//phoneNumbers:[{type=iPhone, number=0123-4567-8888}, {type=home, number=0123-4567-8910}]

III. List’s methods

  • Get element by index
//method get(int index) from List Java
String firstPhone = JsonPath.from(response).get("phoneNumbers.number.get(0)");
  • Find a first element by condition
//method find(Closure) from List groovy (inherite from Collection groovy)
String phone = JsonPath.from(response).get("phoneNumbers.find {it.type == 'iPhone'}.number");
  • Find all elements by condition
//method findAll(Closure) from List groovy
List<String> phone = JsonPath.from(response).get("phoneNumbers.findAll {it.type == 'iPhone'}.number");
  • Only get the list with unique values
//method toUnique() from List groovy
List<String> phone = JsonPath.from(response).get("phoneNumbers.number.toUnique()");
//[0123-4567-8888, 0123-4567-8910]

IV. Summary

Above examples are just some common cases when dealing with json. There are still a lot of methods that you ultilize to satisfy your needs. Hope this article reveals the mysterious of GPath groovy. And don’t forget to leave a like for this post. Thanks

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments