asp008
asp008

Reputation: 147

Match String json array response for each element in Karate

I have an api which returns string json array in response as follows. There are other elements as well in response.

"contacts": [
  "[email protected]",
  "[email protected]"
]

Now I need to match each element in this array to check its value.

In feature file:

When method POST
Then status 200
* match  response.contacts contains ['[email protected]', [email protected]]

But am getting syntax error:

match  response.contacts contains ['[email protected]', [email protected]]

Even I am doing assert response.contacts.[0] == '[email protected]' this also fails. Any help?

Upvotes: 2

Views: 7187

Answers (2)

Peter
Peter

Reputation: 5184

If your karate code

When method POST
Then status 200
* match  response.contacts contains ['[email protected]', [email protected]]

Is the same as you are using in your test, then the syntax error is that you forget to put the second mail in quotes.

Correct:

When method POST
Then status 200
* match  response.contacts contains ['[email protected]', '[email protected]']

The reason why

* assert response.contacts.[0] == '[email protected]'

fails is simple. The dot after contacts is to much.

Correct:

* assert response.contacts[0] == '[email protected]'

Upvotes: 1

Peter Thomas
Peter Thomas

Reputation: 58058

Works for me. You must have some typo or basic mistake somewhere.

* def response = { "contacts": [ "[email protected]", "[email protected]" ] }
* match response.contacts contains [ '[email protected]', '[email protected]' ]
* match response.contacts[1] == '[email protected]'

Upvotes: 1

Related Questions