JCK
JCK

Reputation: 129

Karate: Traverse thru a complex JSON to find a match

I am hitting an API end point and getting something like below.

{
    "emp": {
        "id": "123",
        "firstNm": "test",
        "lastNm": "last",
        "dob": "200-01-01",
        "gender": {
            "code": "F",
            "name": "Female",
            "description": "Female"
        },
        "test1": [
            {
                "tes2": "F50045A3B994FB2BDF4E3D3FC906F592",
                "t2": "a23",
                "test3": {
                    "code": "432",
                },
                "ind": [
                    "ABC",
                    "BCD",
                ]
            }
        ]
    }
}

I want to match the elements in the array

"ind": [
    "ABC",
    "BCD",
]

I have tried the below:

 Feature: test
Background:
        * url BaseUrl
        * configure headers = read('classpath:headers.js')
Scenario Outline: test
    Given path '/path'
    And param id = <id>
    When method get
    Then status 200

        * def json = response
        * def has = {ind:['#string'] }
        * def indicator = { ind: '#[] has' }
        * match json.member == '#[] indicator'

Examples:
    |   id   |
    | '1234' |

But observed the below exception:

expected: '#[] ind', reason: not an array or list Can someone please help?

Upvotes: 1

Views: 214

Answers (1)

coolBeans
coolBeans

Reputation: 58

I am not really following your logic since indicators is not in the json response or defined variable but to get to the ind array the path is emp.test1[0].ind

if you want to match that the array has ABC and BCD you would do the following

* match response.emp.test1[0].ind == ['ABC', 'BCD']

Upvotes: 1

Related Questions