提问者:小点点

JSONPath在空手道中使用时不返回值,但使用在线计算器时返回值


我对JSONPath相当陌生,所以这可能是我的错,但是当我在在线评估器(https://jsonpath.com/)中尝试这个表达式时,它有效,但在空手道中无效。

$..entry[?(@.resource.resourceType == 'AllergyIntolerance' && @.resource.category=='food')].resource.code.coding.*.system

如果我使用索引,我可以取出第一个元素,但我想抓取与表达式匹配的所有元素,而不管它们的索引如何,以防数组中有更多项目而不是我的特定数据示例。

工作JSON路径:

$..entry[?(@.resource.resourceType == 'AllergyIntolerance' && @.resource.category[0]=='food')].resource.code.coding.*.system

我尝试使用通配符,但这似乎不起作用:

$..entry[?(@.resource.resourceType == 'AllergyIntolerance' && @.resource.category[*]=='food')].resource.code.coding.*.system

包含相关部分的 JSON 截图

{
  "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个答案

匿名用户

众所周知,JsonPath引擎在处理如此复杂的表达式时存在问题。请使用< code >空手道.过滤器()来代替,我相信你会同意这是更可读的: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)