Mihir
Mihir

Reputation: 511

Karate - To find array indices of multiple occurance of an element in a JSON response array

I have a JSON response from an endpoint that gives me a nested array of elements. And given an input value, I want to find out all the index values where this value occurs and not just the first occurance of the element.

For example here is the response I have:

{
    "items": [
        {
            "vin": "MMTestingVIN00002",
            "dealerCode": "1",
            "nmscCode": "1",
            "warning": {
                "warningLightType": {
                    "code": 1,
                    "description": "",
                    "symbol": "OLW",
                    "type": "S",
                    "priority": "1"
                }
            }
        },
        {
            "vin": "HESQM0IBWUR7DH0DU",
            "dealerCode": "1",
            "nmscCode": "1",
            "warning": {
                "warningLightType": {
                    "code": 1,
                    "description": "",
                    "symbol": "OLW",
                    "type": "S",
                    "priority": "1"
                }
            }
        },
        {
            "vin": "MMTestingVIN00002",
            "dealerCode": "1",
            "nmscCode": "1",
            "warning": {
                "warningLightType": {
                    "code": 1,
                    "description": "",
                    "symbol": "OLW",
                    "type": "S",
                    "priority": "1"
                }
            }
        },
        {
            "vin": "ZCADWKEQM1GEADEQR",
            "dealerCode": "1",
            "nmscCode": "1",
            "warning": {
                "warningLightType": {
                    "code": 1,
                    "description": "",
                    "symbol": "WASH",
                    "type": "S",
                    "priority": "1"
                }
            }
        },
        {
            "vin": "H5QGE06R54B8KYOUV",
            "dealerCode": "1",
            "nmscCode": "1",
            "warning": {
                "warningLightType": {
                    "code": 1,
                    "description": "",
                    "symbol": "WASH",
                    "type": "S",
                    "priority": "1"
                }
            }
        }
    ]
}

I want to find out the indices where the arrays with "vin = MMTestingVIN00002" occurs.

I looked at https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature for ideas. Also looked at other SO answers and tried:

* def VIN = 'MMTestingVIN00002'
* def response = result.items
* def names = $[*].vin
* def index = names.indexOf(VIN)
* print index

This gives the only the first occurance with the index 0. Ideally I want a result array index[] which gives [0,2] as result.

Upvotes: 1

Views: 1830

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Here you go:

* def indexes = []
* def fun = function(x, i){ if (x.vin == 'MMTestingVIN00002') karate.appendTo(indexes, i) }
* karate.forEach(response.items, fun)

Upvotes: 1

Related Questions