JSONPath does not return values when using in Karate but does using online evaluator

▼魔方 西西 提交于 2021-02-17 04:52:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!