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(

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": "",
            "first_name": "Michael",
            "last_name": "Lawson",
            "avatar": ""
            "id": 8,
            "email": "",
            "first_name": "Lindsay",
            "last_name": "Ferguson",
            "avatar": ""

def pageValue =
def totalValue =
def data =

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


[[avatar:,, first_name:Michael, id:7, last_name:Lawson], [avatar:,, first_name:Lindsay, id:8, last_name:Ferguson]]

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.