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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | { "id":"83072", "employee_name":"83072", "employee_salary":"123", "employee_age":"23", "profile_image":"" }, { "id":"83073", "employee_name":"tyytytytyytyt", "employee_salary":"1888823", "employee_age":"83", "profile_image":"" }, { "id":"83077", "employee_name":"satheesh123", "employee_salary":"123", "employee_age":"23", "profile_image":"" }, { "id":"83083", "employee_name":"tyytytytyfffytyt", "employee_salary":"1888823", "employee_age":"83", "profile_image":"" }, { "id":"83088", "employee_name":"fil", "employee_salary":"123", "employee_age":"23", "profile_image":"" }, { "id":"83096", "employee_name":"testsss", "employee_salary":"123", "employee_age":"23", "profile_image":"" }, { "id":"83103", "employee_name":"tyytytytUUUUyfffytyt", "employee_salary":"1888823", "employee_age":"83", "profile_image":"" }, { "id":"83105", "employee_name":"tyUUUyfffytyt", "employee_salary":"1888999823", "employee_age":"8003", "profile_image":"" }, { "id":"83109", "employee_name":"Test Post call 0.5445519537216511", "employee_salary":"0", "employee_age":"23", "profile_image":"" }, { "id":"83110", "employee_name":"Test Post call 0.525619952355524", "employee_salary":"23000", "employee_age":"29", "profile_image":"" }, ... |
Here is the code to send the GET request to the above-mentioned REST API Service Endpoint and perform JSON parsing using JSONPath:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import static org.testng.Assert.assertEquals; import java.util.List; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; public class JSONParsing { @Test public void testJsonParsing() { RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1"; Response response = null; try { response = RestAssured.given() .when() .get("/employees"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); System.out.println("Status Code :" + response.getStatusCode()); //Creation of JsonPath object JsonPath jsonPathValidator = response.jsonPath(); //1. To print the list of id from the response System.out.println("ID : \n" + jsonPathValidator.get("id")); //2. Count Number of Records(Employees) in the Response List < String > jsonResponseRoot = jsonPathValidator.getList("$"); System.out.println("Number of Employees : " + jsonResponseRoot.size()); //3. Get the list of all the employee names List < String > allEmployeeNames = jsonPathValidator.getList("employee_name"); System.out.println("\n Here is the names of all the employees :\n"); for (String i: allEmployeeNames) { System.out.println(i); } //4. To Get the list of ages of all the employees String employeeAge = jsonPathValidator.getString("employee_age"); System.out.println(employeeAge); //5. To get the name of the sixth employee in the list using 2 ways: //1. String sixthEmployeeName = jsonPathValidator.getString("employee_name[5]"); System.out.println(sixthEmployeeName); //2. System.out.println(allEmployeeNames.get(5)); //6. To validate if the 10th employee salary is greater than 100 if (Integer.parseInt(jsonPathValidator.getString("employee_salary[9]")) > 100) { System.out.println("True - 10th employee salary is " + jsonPathValidator.getString("employee_salary[9]") + " which is greater than 100"); } else { System.out.println("False - 10th employee salary is " + jsonPathValidator.getString("employee_salary[9]") + " which is lesser than 100"); } } } |
Let’s try to understand the code:
1. Setting up Base URI
1 | RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1"; |
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
1 2 3 4 | Response response = null; response = RestAssured.given() .when() .get("/employees"); |
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:
1 2 | //Creation of JsonPath object JsonPath jsonPathValidator = response.jsonPath(); |
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
1 | System.out.println("ID : \n" + jsonPathValidator.get("id")); |
3.2 Count Number of Records(Employees) in the Response (Parsing the list)
1 2 3 | List <String> jsonResponseRoot = jsonPathValidator.getList("$"); System.out.println("Number of Employees : " + jsonResponseRoot.size()); |
3.3 Get the list of all the employee names
1 2 3 4 5 6 7 8 | List < String > allEmployeeNames = jsonPathValidator.getList("employee_name"); System.out.println("\n Here is the names of all the employees :\n"); for (String i: allEmployeeNames) { System.out.println(i); } |
3.4 To Get the list of ages of all the employees
1 2 3 | String employeeAge = jsonPathValidator.getString("employee_age"); System.out.println(employeeAge); |
3.5 To get the name of the sixth employee from the list using 2 ways:
1 2 3 4 5 6 7 8 | //1. String sixthEmployeeName = jsonPathValidator.getString("employee_name[5]"); System.out.println(sixthEmployeeName); //2. System.out.println(allEmployeeNames.get(5)); |
3.6 To validate if the 10th employee salary is greater than 100
1 2 3 4 5 6 7 8 | if (Integer.parseInt(jsonPathValidator.getString("employee_salary[9]")) > 100) { System.out.println("True - 10th employee salary is " + jsonPathValidator.getString("employee_salary[9]") + " which is greater than 100"); } else { System.out.println("False - 10th employee salary is " + jsonPathValidator.getString("employee_salary[9]") + " which is lesser 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:
Hi Deepak,
I am getting error on this code please guide me
//2. Count Number of Records(Employees) in the Response
List jsonResponseRoot = jsonPathValidator.getList(“$”);
System.out.println(“Number of Employees : ” + jsonResponseRoot.size());
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.util.List (java.util.LinkedHashMap and java.util.List are in module java.base of loader ‘bootstrap’)
Hello Vaibhav,
Sorry for the inconvenience.
http://dummy.restapiexample.com/ is throwing ‘Origin error’ while accessing right now. I am not sure if your problem is coming because of that.
Could you please first check if the site is up for you or not?
Thanks