如何使用jsonPath过滤空手道dsl中的复杂响应



我从REST API获得的响应低于此值,但我发现很难从接收到的响应中提取标签值,并将其分配给一个变量以便稍后在脚本中使用
这是响应::

{
"result": "SUCCESS",
"rawAttr": "[{"attributes":[{"name":"resourceid","value":"7A7Q123456"},{"name":"physicalid","value":"7A7Q123456"},{"name":"dsw:label","value":"MY Product00004285"},{"name":"dsw:created","value":"2019-11-06T08:39:39Z"}]}]",
"physicalid": "7A7Q123456",
"contextPath": "/path",
"id": "7A7Q123456",
"message": null
}

我能得到response.idresponse.result,这有助于验证,但我不能得到dsw:labelvalue,这是MY Product00004285

当我执行def Arr = response.rawAttr时,无论是Array还是String,我都会得到以下值。看起来像是一根绳子。

[{"attributes":[{"name":"resourceid","value":"7A7Q123456"},{"name":"physicalid","value":"7A7Q123456"},{"name":"dsw:label","value":"MY Product00004298"},{"name":"dsw:created","value":"2019-11-06T08:39:39Z"}]}]

使用以下JSON路径表达式在JMeter JSON提取器中提取标签非常容易$.attributes.value[2]

参考空手道的类型转换能力:https://github.com/intuit/karate#type-转换

所以你可以这样做:

* json attr = response.rawAttr

然后你们都准备好了。

感谢一个将字符串转换为json的示例和文档
知道怎么做了。

And def strVar = response.rawAttr
And json jsonVar = strVar
And def attrb = karate.jsonPath(jsonVar, '$..attributes.[2].value')[0]
And print 'nn Attrbn', attrb

我引用的链接:
Json路径评估器
用于类型转换的空手道文档参考
类型转换的空手道示例

最新更新