如何在 karate.filterKeys 中提供带有特殊字符的键?

How to provide keys with special char in karate.filterKeys?

回复:

[
    {
        "key1":78,
        "key2":"value2",
        "key3": {
            "obj.key1":"objValue1",
            "obj.key2":1236527454,
        }
    },
    {
        "key1":89,
        "key2":"value2",
        "key3": {
            "obj.key1":"objValue1",
            "obj.key2":9885546755,
        }
    }
]

预期响应:

[
    {
        "key1":32,
        "key2":"value2",
        "key3": {
            "obj.key1":"objValue1",
            "obj.key2":8985436,
        }
    },
    {
        "key1":36,
        "key2":"value2",
        "key3": {
            "obj.key1":"objValue1",
            "obj.key2":655431,
        }
    }
]

key1, obj.key2 是动态值,在比较 JSON.

的其余部分时需要忽略这些值
* match karate.filterKeys(response,['key1']) == karate.filterKeys(expectedResponse,['key1'])

它按预期工作并且在比较结果时忽略了 key1。

* match karate.filterKeys(response,['key1','key3["obj.key2"]') == karate.filterKeys(expectedResponse,['key1','key3["obj.key2"]']) 

以上抛出错误。我也试过使用:

* match karate.filterKeys(response,['key1',key3['obj.key2']) == karate.filterKeys(expectedResponse,['key1',key3['obj.key2']]) 

因为这通常是空手道中使用特殊字符键的方式,但再次出现错误。

有人可以解决这个问题吗?

我推荐一种不同的方法,因为你有一个复杂的嵌套并且 JSON 有可怕的键。使用 JSON transforms 以您想要的“形状”获取数据,然后事情变得简单。

* def fun = 
"""
function(x) {
  var y = {};
  y.key2 = x.key2;
  y.key3 = {};
  y.key3['obj.key1'] = x.key3['obj.key1'];
  return y;
}
"""
* def temp = karate.map(response, fun)
* match each temp == { key2: 'value2', key3: { 'obj.key1': 'objValue1' } }