Get Response Body in REST-Assured using JSONPath

This entry is part 15 of 14 in the series REST Assured Tutorial

In previous posts we have seen how to send different kinds of request and extracting data, and validating it. Now we will dive deeper in REST Assured. We are going to learn how to get Response Body in Rest Assured using JsonPath. Ever seen huge JSON response ?. If you have a large amount of JSON response, validating it with string can be scary, because we are only interested in a few fields out of that huge response and not all of it. You can reach to a specific field with JSONPath. Lets consider below API :

The API returns below response :

We will try to validate if the “x-forwarded-port” has a value “443” or not. We will get that field with JsonPath by simply providing a path to that field. Look closely at the response, if we want to reach to “x-forwarded-port”, how do we reach. We can clearly see the path to it as
“header” >> “x-forwarded-port”
No matter how deep our required field is, we can reach to it by providing its path. In JsonPath you have to provide the path like below :

Lets do it. You need to import JsonPath for that like below

Here is our working program :

The output of the program:

Let me know in comments below, which topic you would like to see for Rest-Assured Tutorial.

Series Navigation<< Send Query Parameter using REST-Assured