In this Post we will learn How to parse Json Response Body using Rest Assured, but before that please have a look of my previous tutorials Create First Project by RestAssured.
First, let me create a project where I will use POST method to post the resource on the server and get response in String format.
Table of Contents
How to Read Json Response Body-
package demo; import io.restassured.RestAssured; import io.restassured.response.Response; import static io.restassured.RestAssured.*; import org.testng.Assert; import files.payload; import static org.hamcrest.Matchers.*; public class Basics { public static void main(String[] args) { // TODO Auto-generated method stub RestAssured.baseURI="https://rahulshettyacademy.com"; String response=given().log().all().queryParam("key", "qaclick123").header("Content-Type", "application/json") .body(payload.AddPlace()) .when().post("maps/api/place/add/json") .then().assertThat().statusCode(200).body("scope", equalTo("APP")) .header("server", "Apache/2.4.52 (Ubuntu)").extract().response().asString(); System.out.println(response); } }
Explanation: In above code, we can see using the POST method I have Post a payload on the server and validate status code, body content and header details.
Using the extract method get the response and using asString() method convert the response into String format. Print the response using System.out.println().
How to Extract a Node text from Response using JsonPath?
Now I will parse the JSON Response using JsonPath class. This is need when we want to read something attributes values from Json response and need to validate it.
Code-
package demo; import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; import static io.restassured.RestAssured.*; import org.testng.Assert; import files.payload; import static org.hamcrest.Matchers.*; public class Basics { public static void main(String[] args) { // TODO Auto-generated method stub RestAssured.baseURI="https://rahulshettyacademy.com"; String response=given().queryParam("key", "qaclick123").header("Content-Type", "application/json") .body(payload.AddPlace()) .when().post("maps/api/place/add/json") .then().assertThat().statusCode(200).body("scope", equalTo("APP")) .header("server", "Apache/2.4.52 (Ubuntu)").extract().response().asString(); System.out.println(response); //How to Extract a Node text from Response using JsonPath? JsonPath js = new JsonPath(response); // for parsing json String placeid=js.getString("place_id"); System.out.println("Place ID is : " +placeid); } }
How to Use PUT method in Rest Assured?
In above example we get the place using place id. Now we will update that place with new address using PUT method. PUT method is generally used for updating the resource on the server.
Code-
package demo; import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import static io.restassured.RestAssured.*; import files.payload; import static org.hamcrest.Matchers.*; public class Basics { public static void main(String[] args) { // TODO Auto-generated method stub RestAssured.baseURI="https://rahulshettyacademy.com"; String response=given().queryParam("key", "qaclick123").header("Content-Type", "application/json") .body(payload.AddPlace()) .when().post("maps/api/place/add/json") .then().assertThat().statusCode(200).body("scope", equalTo("APP")) .header("server", "Apache/2.4.52 (Ubuntu)").extract().response().asString(); System.out.println(response); //How to Extract a Node text from Response using JsonPath? JsonPath js = new JsonPath(response); String placeid=js.getString("place_id"); System.out.println("Place ID is : " +placeid); // update place using PUT Method String newAddress = "Summer Walk, Africa"; given().log().all().queryParam("key", "qaclick123").header("Content-Type", "application/json") .body("{\r\n" + "\"place_id\":\""+placeid+"\",\r\n" + "\"address\":\""+newAddress+"\",\r\n" + "\"key\":\"qaclick123\"\r\n" + "}") .when().put("maps/api/place/update/json") .then().assertThat().log().all().statusCode(200).body("msg", equalTo("Address successfully updated")); } }
Output-
Summary:
In this post, we have covered ‘How to Parse JSON Response Body using Rest Assured’ using java coding in Eclipse.