0

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

kaweesha
  • 803
  • 6
  • 16
Manish Saini
  • 5
  • 1
  • 3

3 Answers3

0

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.

kaweesha
  • 803
  • 6
  • 16
0

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;
    }
}
oktaykcr
  • 346
  • 6
  • 12
-1

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.