rest-assuredrestjayway

Can we use io.restassured and jayway in the same method?


When I have arrays in response, getting proper results using Jayway, but not with io.restassured ? Can I use Jayway and io.restassured together ? Is that an acceptable / good practice?

JSON Response :

   {"applications": [
      {
      "Id": "123",
      "amount": "1500"
   },
      {
      "Id": "456",
      "amount": "2500"
   },
      {
      "Id": "780",
      "amount": "3500"
   }
]}

Looking for amount 2500 as my result! Tried below: //1st approach to read response form json body JsonPath jsonPath = res.jsonPath(); System.out.println(jsonPath.get("$.applications[1].amount")); //results null, using io.restassured JsonPath

//2nd approach to read response form json body JsonPath jsonPath1 = JsonPath.from(res.asString()); System.out.println(jsonPath1.getString("$.applications[1].amount")); //results null, using io.restassured JsonPath

//3rd approach to read response form json body System.err.println(JsonPath.read(res.asString(),"$.login")); // results 2500, using jaywayJsonPath


Solution

  • There are multiple ways of extracting the values

        // Method 1
        String res = given().when().get("http://soapractice1.mocklab.io/thing/test").then().extract().asString();
        JsonPath js = new JsonPath(res);
        System.out.println("The amount is : " + js.get("applications[1].amount"));
    
        // Method 2
        Response resp = given().when().get("http://soapractice1.mocklab.io/thing/test").then().extract().response();
        JsonPath js1 = resp.jsonPath();
        System.out.println("The amount is : " + js.get("applications[1].amount"));
    
        // Method 3
        String amount = given().when().get("http://soapractice1.mocklab.io/thing/test").then().extract().jsonPath().get("applications[1].amount");
        System.out.println("The amount is : " + amount);