In this tutorial, our objective is to extract or read the response from the request we made (GET, POST, etc.) and then perform the validation or assertions on that. Here, we are going to see “How to parse JSON response body object in Rest-Assured using JsonPath?”

 

 

Simple representation of Parsing JSON Response using RESTAssured...!!! Share on X

In this example, we will use the ‘Dummy Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purposes which are performing various CRUD operations.

Let’s take an example of one of the API GET endpoints available at the above-mentioned website which is ‘/employees’.

The full-service URL with an endpoint is ‘http://dummy.restapiexample.com/api/v1/employees‘.

At the above resource URL, information about all the employees is present and now we are trying to access those employee’s details & parse it using JSONPath in this below example like id, employee_name, employee_age, employee_salary, and profile_image.

 

Below is some portion of JSON Response body which will be returned after sending a get request to the above-mentioned service endpoint: (Not showing full JSON as it’s pretty big)

 

Here is the code to send the GET request to the above-mentioned REST API Service Endpoint and perform JSON parsing using JSONPath:

 

Let’s try to understand the code:

1. Setting up Base URI

Base URI is the root address of the Resource. And, by this particular line of code, we are specifying to REST assured to use “dummy.restapiexample.com/api/v1” as the root URL of the service.

2. Specifying the exact resource to look for and make a GET request to that resource

Here, using this code, we are looking to make a GET request to an exact resource which is “/employees” in this case. Hence, the complete Service Endpoint would be “http://dummy.restapiexample.com/api/v1/employees” to which we are sending a GET request. And, the output of the GET call would be stored in the REST Assured ‘Response’ object.

3. Response Body Parsing using JSONPath

First, we are building a jsonPath object using below code:

 

Now, we are going to show you some different variety of validations as a sample that would help you to understand how JSONPath can be used to parse the JSON and perform validation.

3.1 Print the list of id from the response

 

3.2 Count Number of Records(Employees) in the Response (Parsing the list)

 

3.3 Get the list of all the employee names

 

3.4 To Get the list of ages of all the employees

 

3.5 To get the name of the sixth employee from the list using 2 ways:

 

3.6 To validate if the 10th employee salary is greater than 100

 

In this way, you can use JSONPath and it’s very effective and helpful and quite simple to implement and write validations around it.

That’s it, it’s that simple to parse the JSON Response body using JSONPath in REST Assured API: ?

 

Simple representation of Parsing JSON Response using RESTAssured...!!! Share on X

 

If you like this post, please check out my other useful blog posts on Rest Assured:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak