'Karate - Can't print values from the response (array of JSON objects)

I have a test in Karate where the response is an array of JSON objects, all of them include a key called id. I need to print all those values but I can't figure out how.

I can print a single value by 'And print response[0].id' but I can't make it work for all the id.

I've tried:

print each response.id

print each response..id



Solution 1:[1]

Please split into 2 steps. This below is using JsonPath: https://github.com/karatelabs/karate#get

* def temp = $response[*].id
* print temp

This may work (pure JavaScript):

* print response.map(x => x.id)

For completeness, you can do this:

* print karate.jsonPath(response, '$[*].id')
# or
* print karate.get('$response[*].id')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Thomas