简体   繁体   中英

how to validate Json Response value in Rest Assured

Hi i am getting Json Response Body from a get request in REST request.

{
  "outputData": {
    "data": {
      "leaveList": [
        {
          "leaveTypeID": 1,
          "leaveBalance": 2
        },
        {
          "leaveTypeID": 2,
          "leaveBalance": 9
        }
      ]
    }
  }
}

I want to get leaveBalance of leaveType 2 . Index of the leaveTypeID keep changing.

Thanks

You can do it by iterating through leaveList JsonArray.

    Response response = given()
            .when()
            .get("URL")
            .then()
            .extract()
            .response();

    int leaveTypeId = 2; // You can change this get the required leaveBalance
    int leaveBalance = 0;

    JSONObject responseObject = new org.json.JSONObject(response.body().asString());

    JSONObject object1 = responseObject.getJSONObject("outputData");
    JSONObject dataObject = object1.getJSONObject("data");
    JSONArray jsonArray = dataObject.getJSONArray("leaveList");

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        if (jsonObject.get("leaveTypeID").equals(leaveTypeId)) {
            leaveBalance = Integer.parseInt(jsonObject.get("leaveBalance").toString());
        }
    }

    System.out.println("leaveBalance = " + leaveBalance);

This will print;

leaveBalance = 9

You can assign values dynamically to leaveTypeId and get the leaveBalance you want.

You can use jackson-databind library for converting json string to object. I used maven package manager to add into my project.

I suggest the following solution to provide a well structure in your project. In my solution I used classes to keep json values. You can use Json To Pojo to generate objects from json string.

The output: 9

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

public class RestAssuredDemo {

    public static void main(String[] args) {
        String json = """
                        {
                          "outputData": {
                            "data": {
                              "leaveList": [
                                {
                                  "leaveTypeID": 1,
                                  "leaveBalance": 2
                                },
                                {
                                  "leaveTypeID": 2,
                                  "leaveBalance": 9
                                }
                              ]
                            }
                          }
                        }
                       """;

        ObjectMapper om = new ObjectMapper();
        Root root = null;
        try {
             root = om.readValue(json, new TypeReference<Root>() {});
             for (LeaveList leave: root.outputData.data.leaveList) {
               if(leave.leaveTypeID == 2) {
                  System.out.println(leave.leaveBalance);
               }
             }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public static class LeaveList{
        public int leaveTypeID;
        public int leaveBalance;
    }

    public static class Data{
        public List<LeaveList> leaveList;
    }

    public static class OutputData{
        public Data data;
    }

    public static class Root{
        public OutputData outputData;
    }
}

GroovyPath = "outputData.data.leaveList.find{it.leaveTypeID==2}.leaveBalance"

String leaveBalance = response.extract().jsonPath().getString("outputData.data.leaveList.find{it.leaveTypeID==2}.leaveBalance");

it will provide you the leave balance of where leaveTypeID is 2 index of this object can be anywhere this will work.

Here is very good article written on how to validate json response in restassured using jsonPath. However there are other ways as well to validate response such as using POJO class. That is also explained in same blog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM