How to find an object by a field value in Rest Assured?

1.6k Views Asked by At

I need to get to code of an array element that has field == firstName:

{
    "errors": [
        {
            "field": "firstName",
            "code": "NotBlank"
        },
        {
            "field": "lastName",
            "code": "NotBlank"
        }
    ]
}

With JsonPath, the way to do it is $.errors[?(@.field == firstName)].code. How do I do the same in Rest Assured with its GPath syntax?

2

There are 2 best solutions below

0
On BEST ANSWER

Haven't tried it but from the top of my head this ought to work:

errors.find { it.field == 'firstName' }.code
0
On

errors.find{e -> e.field == 'firstName'}.code