Home » restAssured-tutorial » Parse JSON Response Body with Rest Assured

Newly Updated Posts

Parse JSON Response Body with Rest Assured

In Previous tutorial, we have learned how to Test Rest Api using Rest Assured so in current tutorial we will move to more advance topic and understand Parse JSON Response Body with Rest Assured.

How to Parse JSON Response Body with Rest Assured?

To under in depth, we will parse Json mock response body with two differnet type of json body, firstly we will consider a simple json response after that we will take nested json response body and try to parse with different sets of scenarios.

1) Simple Json : To parse json body ,we will be using JsonPath class and be using its methods to get the value of a particular attribute.

Below is the mock json response

{
"status": "OK",
"place_id": "9172e95034d47483d8e0775710eb6a54",
"scope": "APP",
"reference": "de2fad9729217c7b69d9a0bb4906e742de2fad9729217c7b69d9a0bb4906e742",
"id": "de2fad9729217c7b69d9a0bb4906e742"
}

Suppose we required to retrieve the value of place_id from the above mock json response, so to get the vlaue of “place_id ” below is the code for the same.

JsonPath jpath=new JsonPath(response);
String placeId=jpath.getString("place_id");

Complete code in eclipse with be look like this:

Parse JSON Response Body with Rest Assured

2) Nested Json: Json can contain nested objects in Json format and these object may contains Array with value assigned to it key in key value form this type of json structure is known as nested Json.

Below is the mock json response example:

{
"dashboard": {
"purchaseAmount": 1060,
"website": "code2test.com"
},
"courses": [
{
"title": "Selenium",
"price": 50,
"copies": 6
},
{
"title": "Appium",
"price": 40,
"copies": 4
},
{
"title": "Rest Assured",
"price": 45,
"copies": 10
},
{
"title": "SoapUI",
"price": 30,
"copies": 5
}
]
}

To view or under json in better way, you can can open https://jsoneditoronline.org/ website and paste above json file it will give a tree structre as shown below.

Json structure in jsoneditor

Lets parse the above json with all possible ways by considering the below scenarios.

a) Get the purchase amount of the course?

b) Title of first course (which is selenium) ?

c) Total copies sold by SoapUI?

d) Total number of courses?

e) Print all the title of the courses

Lets start above sechanario sequesncially.

a) Get the purchase amount of the course

JsonPath jpath=new JsonPath(response);		
int puchaseAmt=jpath.getInt("dashboard.purchaseAmount");

b) Title of first course (which is selenium)

JsonPath jpath=new JsonPath(response);
String firstCourseTitle=jpath.getString("courses[0].title");

c) Total copies sold by SoapUI

JsonPath jpath=new JsonPath(response);
String totalCopiesSoapUi=jpath.getString("courses[3].copies");

d) Total number of courses?

JsonPath jpath=new JsonPath(response);
int sizeofCourse=jpath.getInt("courses.size()");

e) Print all the title of the courses

JsonPath jpath=new JsonPath(response);
int sizeofCourse=jpath.getInt("courses.size()");
for(int i=0;i<sizeofCourse;i++) 
{
String alltitle=jpath.getString("courses["+i+"].title");
System.out.print(alltitle+", ");
	
}

Complete code in eclipse will look in below

Parse JSON Response Body with Rest Assured

Conclusion

So in the above tutorial we have learned to parse a simple and complex json or nested json , this parsing of json is very helpful when we hit any rest api and get the json response, in that case we need to retrieve the value from json in key value pair.