REST Assured Fetch JSON using Groovy's GPath

Profile picture for user arilio666

In this article, we will look at the JSON path implementation that uses groovy's GPath syntax. By default REST Assured uses JSON path implementation, so we must get very familiar with this.

Apache Groovy is a powerful, optionally typed dynamic language that is a multi-faceted java platform. Groovy comes as integrated support for converting between groovy objects and JSON. JSON serialization classes and parsing come under this groovy.json package.

JSON slurper inside package groovy.json, is the default class used here to parse JSON text or other reader content into Groovy data structures such as map, list, integer, string boolean, etc. You can read more about JSON slurper class here.

The GPath is a path expression language integrated into the groovy language. It has similar scope that of Xpath and XML. It used dot-object notation to perform object navigation in JSON

For Example

  • x.y.z -> For XML yields all the z elements inside x and y.
  • x.y.z -> In POJO yields z properties for all y properties of x.

This is the sort of navigation followed by GPath in groovy JSON path. You can read about GPath expression here.

JsonSlurper Sample Code for Groovy

Now enough talk, let us dive into a groovy-based JSON object path navigation. You do not need to learn Groovy, just paste below code in any of following Groovy compiler and replace your JSON with INSERT_YOUR_JSON_HERE.

import groovy.json.JsonSlurper

def object = new JsonSlurper().parseText(
'''
INSERT_YOUR_JSON_HERE
'''
)

def query = object
println query
  • This is the groovy language where we are importing our JSON Slurper class.
  • Then we create a new class instance and use the parse text method to parse JSON, which converts the JSON object into a groovy thing.
  • Then we can use the GPath expression to fetch individual objects.

Let us dive right in.

Example: Parson JSON using Groovy GPath

import groovy.json.JsonSlurper

def object = new JsonSlurper().parseText(
'''
{
    "page": 2,
    "total": 12,
    "data": [
        {
            "id": 7,
            "email": "michael.lawson@reqres.in",
            "first_name": "Michael",
            "last_name": "Lawson",
            "avatar": "https://reqres.in/img/faces/7-image.jpg"
        },
        {
            "id": 8,
            "email": "lindsay.ferguson@reqres.in",
            "first_name": "Lindsay",
            "last_name": "Ferguson",
            "avatar": "https://reqres.in/img/faces/8-image.jpg"
        }
    ]
}
'''
)

def pageValue = object.page
def totalValue = object.total
def data = object.data

println pageValue
println totalValue
println data
println data.email
println data[0].email
println data[1].email

Output:

2
12
[[avatar:https://reqres.in/img/faces/7-image.jpg, email:michael.lawson@reqres.in, first_name:Michael, id:7, last_name:Lawson], [avatar:https://reqres.in/img/faces/8-image.jpg, email:lindsay.ferguson@reqres.in, first_name:Lindsay, id:8, last_name:Ferguson]]
[michael.lawson@reqres.in, lindsay.ferguson@reqres.in]
michael.lawson@reqres.in
lindsay.ferguson@reqres.in

REST Assured: Fetch JSON using Groovy's GPath 

  • So using this response, we can use it as validation in our java rest assured for JSON fetching.
  • Here we can use the groovy object 'object,' which is a gpath expression, to use and navigate the things to get a response.
  • 2 is the value for page and 12 is for total key.
  • Here, data is an array so we have used 0 and 1 index to print the email of first and seconds JSON object respectively. If you will not use any index with data it will print email for both JSON objects.

So these are some of the basic queries used to fetch JSON objects using groovy gpath.