问题
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.
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 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"
} ]
}
}
} ]
}
回答1:
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)
来源:https://stackoverflow.com/questions/62894708/jsonpath-does-not-return-values-when-using-in-karate-but-does-using-online-evalu