Matt
Matt

Reputation: 1043

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"
                }
            ]
        }
    ]
}

Upvotes: 2

Views: 2114

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Here you go:

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

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

Upvotes: 2

Related Questions