空手道:遍历复杂的JSON查找匹配项

时间:2019-10-09 20:30:56

标签: karate

我正在达到一个API终点,并得到如下所示的内容。

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

我想匹配数组中的元素

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

我尝试了以下方法:

 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' |

但是观察到以下异常:

预期:'#[] ind',原因:不是数组或列表 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我并没有真正遵循您的逻辑,因为指标不在json响应或定义的变量中,而是到达emp数组的路径是emp.test1 [0] .ind

如果要匹配具有ABC和BCD的阵列,请执行以下操作

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