0

在下面的 JSON 示例中,如何查找包含字符串“Choice”的所有元素并将它们替换为另一个字符串,例如“Grade”。因此,下面所有字段名称“***Choice”应更改为“***Grade”。

我在下面粘贴了预期的输出。鉴于我不知道有多少字段将包含字符串“Choice”,我不想简单地[$in ~> | ** [firstChoice] | {"firstGrade": firstChoice}, ["firstChoice"] | ;]直接查找和替换。

{
  "data": {
    "resourceType": "Bundle",
    "id": "e919c820-71b9-4e4b-a1c8-c2fef62ea911",
    "firstChoice": "xxx",
    "type": "collection",
    "entry": [
      {
        "resource": {
          "resourceType": "Condition",
          "id": "SMART-Condition-342",
          "code": {
            "coding": [
              {
                "system": "http://snomed.info/sct",
                "code": "38341003",
                "display": "Essential hypertension",
                "firstChoice": "xxx"
              }
            ],
            "text": "Essential hypertension"
          },
          "clinicalStatus": "active",
          "secondChoice": "xxx"
        },
        "search": {
          "mode": "match"
        }
      }
    ]
  }
}

预期产出

{
  "data": {
    "resourceType": "Bundle",
    "id": "e919c820-71b9-4e4b-a1c8-c2fef62ea911",
    "firstGrade": "xxx",
    "type": "collection",
    "entry": [
      {
        "resource": {
          "resourceType": "Condition",
          "id": "SMART-Condition-342",
          "code": {
            "coding": [
              {
                "system": "http://snomed.info/sct",
                "code": "38341003",
                "display": "Essential hypertension",
                "firstGrade": "xxx"
              }
            ],
            "text": "Essential hypertension"
          },
          "clinicalStatus": "active",
          "secondGrade": "xxx"
        },
        "search": {
          "mode": "match"
        }
      }
    ]
  }
}
4

2 回答 2

1

可能有更简单的方法,但这是我在 JSONata 中提出的一个表达式:

(
  $prefixes := $keys(**)[$ ~> /Choice$/].$substringBefore('Choice');
  $reduce($prefixes, function($acc, $prefix) {(
    $choice := $prefix & "Choice";
    $acc ~> | ** [$lookup($choice)] | {$prefix & "Grade": $lookup($choice)}, [$choice] |
  )}, $$)
)

它看起来很糟糕,但我会解释我是如何构建它的。

你从表达式开始

$ ~> | ** [firstChoice] | {"firstGrade": firstChoice}, ["firstChoice"] |

如果您只想替换一个选项并且您知道全名,这很好。如果要替换多个,则可以将它们链接在一起,如下所示:

$ ~> | ** [firstChoice] | {"firstGrade": firstChoice}, ["firstChoice"] |
  ~> | ** [secondChoice] | {"secondGrade": secondChoice}, ["secondChoice"] |
  ~> | ** [thirdChoice] | {"thirdGrade": thirdChoice}, ["thirdChoice"] |

此时,您可以创建一个接受选择前缀并返回部分替换的高阶函数(注意|...|...|语法会生成一个函数)。$reduce()然后,您可以使用内置的高阶函数将它们链接在一起以获得前缀数组。所以你会得到这样的东西:

(
  $prefixes := ["first", "second", "third"];
  $reduce($prefixes, function($acc, $prefix) {(
    $choice := $prefix & "Choice";
    $acc ~> | ** [$lookup($choice)] | {$prefix & "Grade": $lookup($choice)}, [$choice] |
  )}, $$)
)

但是,如果您事先不知道前缀集,并且想要选择所有以“Choice”结尾的属性名称,则以下表达式将为您提供:

$prefixes := $keys(**)[$ ~> /Choice$/].$substringBefore('Choice')

然后到达我的最终表达。您可以在练习器中对您的数据进行试验。

于 2019-11-22T09:13:51.393 回答
0

我知道那是 2019 年,但这里有一个替代解决方案可以帮助未来的读者。

$replace($string(),/([first|second|third])Choice/,"$1Grade")~>$eval()

于 2021-11-18T10:36:51.363 回答