Search code examples
javagroovyrest-assured-jsonpath

Rest Assured - Extract a single value from a list of multiple records with GPath


I have a list of records:

{
  "StatusCode": 200,
  "Result": [
    {
      "Id": 15015600,
      "Amount": 97.41,
      "CreatedDate": "10/17/2018",
    },
    {
      "Id": 15015602,
      "Amount": 682.11,
      "CreatedDate": "10/17/2018",
    },
   and so on...

I'm trying to craft a statement to return the "Id" value when I know the Amount and CreatedDate.

int Id = given()
            .when()
                .get(/EndPoint))
            .then()
                .body("Result.findAll { it.Amount==97.41 }.CreatedDate", hasItems("10/17/2018"));

Is this even possible?


Solution

  • The solution:

    int i = response.path("Result.find{it.Amount.toDouble()==293.51 && it.CreatedDate=='10/26/2018'}.Id");
    

    I needed to add, "toDouble()" to my query. it.Amount.toDouble()==293.51, not it.Amount==293.51. Once the toDouble() was added the query worked as expected.