在空手道中使用时,JSONPath不返回值,但在使用在线评估器时,确实会返回值 [英] JSONPath does not return values when using in Karate but does using online evaluator
问题描述
我对JSONPath相当陌生,所以这可能是我的错,但是当我在在线评估程序中尝试使用此表达式时( https: //jsonpath.com/),但在空手道中无效.
I'm fairly new to JSONPath so this could be my fault but when I try this expression in an online evaluator (https://jsonpath.com/) it works but does not in Karate.
$..entry[?(@.resource.resourceType == 'AllergyIntolerance' && @.resource.category=='food')].resource.code.coding.*.system
如果使用索引,我可以取出第一个元素,但是我想获取与表达式匹配的所有元素,无论它们的索引如何,以防数组中有更多项而不是我的特定数据示例.
If I use an index I am able to get the first element out but I want to grab all elements that match the expression regardless of their index in case there are more items in the array and not my specific data example.
有效的JSONPath:
Working JSONPath:
$..entry[?(@.resource.resourceType == 'AllergyIntolerance' && @.resource.category[0]=='food')].resource.code.coding.*.system
我尝试使用通配符,但这似乎不起作用:
I've tried to use wildcards but that doesn't seem to work:
$..entry[?(@.resource.resourceType == 'AllergyIntolerance' && @.resource.category[*]=='food')].resource.code.coding.*.system
带有相关部分的JSON代码段
JSON snippit with relevant sections
{
"entry": [ {
"resource": {
"resourceType": "AllergyIntolerance",
"id": "allergyFood",
"category": [ "food" ],
"criticality": "high",
"code": {
"coding": [ {
"system": "http://snomed.info/sct",
"code": "91935009",
"display": "Allergy to peanuts"
} ],
"text": "Allergy to peanuts"
},
"reaction": [ {
"manifestation": [ {
"coding": [ {
"system": "http://snomed.info/sct",
"code": "271807003",
"display": "skin rash"
} ],
"text": "skin rash"
} ],
"severity": "mild"
} ]
}
}, {
"resource": {
"resourceType": "AllergyIntolerance",
"id": "allergyMed",
"verificationStatus": "unconfirmed",
"type": "allergy",
"category": [ "medication" ],
"criticality": "high",
"code": {
"coding": [ {
"system": "http://www.nlm.nih.gov/research/umls/rxnorm",
"code": "7980",
"display": "penicillin"
} ]
}
}
} ]
}
推荐答案
已知JsonPath引擎存在此类复杂表达式的问题.请改用karate.filter()
,我相信您会同意它更具可读性: https://github.com/intuit/karate#json-transforms
The JsonPath engine is known to have issues with such complex expressions. Please use karate.filter()
instead which I am sure you will agree is much more readable: https://github.com/intuit/karate#json-transforms
* def resources = $..resource
* def fun = function(x){ return x.resourceType == 'AllergyIntolerance' && x.category[0] == 'food' }
* def temp = karate.filter(resources, fun)
这篇关于在空手道中使用时,JSONPath不返回值,但在使用在线评估器时,确实会返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!