R
R
Romi2022-04-05 17:25:37
PHPUnit
Romi, 2022-04-05 17:25:37

How do I test that the JSON contains the desired key-value pair in ANY array element?

For example JSON-response in Laravel test:

{"idarray": [
{"id":"2"},
{"id":"55"},
{"id":"32"}
]}

and I need to test what it contains
"id" => "32"
. If I know the serial number of the array element - no question, I can use both assertJson() and assertJsonPath('idarray.2.id', '32') - it works.

But what if I don't know the array element number?

'*' - does not work.

Tried to convert JSON to an array, and then use the methods of PHPUnit itself - it also somehow does not work.

What to do?

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2022-04-05
@romicohen

https://laravel.com/docs/8.x/http-tests#assert-jso...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question