'Karate - How count number of instances of element in JSON response with embedded elements

I want to work out the total number of occurences of 'id' in the following JSON String.

Does Karate have a quick way of doing this?

If it was at the top level I could do response.result.length but they are are in the embedded elements of 'test'. I could do this in javascript but just wondering if Karate has a quicker method.

{
    "result": [
        {
            "test": [
                {
                    "id": "x",
                    "price": "£5.00"
                },
                {
                    "id": "y",
                    "price": "£10.00"
                },
                {
                    "id": "z",
                    "price": "£10.00"
                },
                {
                    "id": "a",
                    "price": "£10.00"
                }
            ]
        },
        {
            "test": [
                {
                    "id": "b",
                    "price": "£5.00"
                },
                {
                    "id": "c",
                    "price": "£10.00"
                }
            ]
        }
    ]
}


Solution 1:[1]

Here you go:

* def ids = $..id
* assert ids.length == 6

Do take some time to read about JsonPath in the docs.

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