0

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

Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
learningIsFun
  • 142
  • 1
  • 9

1 Answers1

1

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);
Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
  • all 3 methods throwing "java.lang.IllegalArgumentException: Cannot invoke method getAt() on null object" error – learningIsFun Jul 27 '20 at 14:20
  • I'm not sure how you have `getAt()`, recheck the code. remove the jayway dependency and make sure you are on the latest version of `io.restassured`. I have tried this locally and its working – Wilfred Clement Jul 27 '20 at 14:21
  • Still same error after removing jayway dependencies and updating to latest io.restassured – learningIsFun Jul 27 '20 at 14:34
  • 1
    I have updated the answer but only with the URL which is a working example, If you copy this code and paste it in your IDE and execute it then it will work unless you are making a mistake – Wilfred Clement Jul 27 '20 at 15:54