Dataweave 根据另一个 JSON 字段更新一个 JSON 字段

问题描述 投票:0回答:2

我想通过比较第二个 JSON 来更新 payload.title 值。在下面的 payload.title 值

'Mr.'
应该更新为 secondJson
xrefValue
'Mister'
。比较条件是 payload.value 应该等于
secondJson.masterObjectValues[0].code
,然后 payload.title 值应该更新为 xrefValue 值,其中
systemCode
=
SYS2
.

你能帮我用数据编织语言对这个进行排序吗?

有效载荷=

{
"patient": {
"patientNumber": "123",
"secondaryAltPatientIdentifier": "2222222",
"title": "Mr.",
"preferredName": "test"
}
}

secondJson=

{
  "objectCode": "Title",
  "masterObjectValues": [
    {
      "systemCode": "MASTER",
      "systemObjectValues": [
        {
          "codeId": "a5G9p0000001HwTEAU",
          "code": "Dr.",
          "crossRef": [
            {
              "systemCode": "BBS",
              "xrefValue": "DR"
            },
            {
              "systemCode": "NAVSYD",
              "xrefValue": "DR"
            }
          ]
        },
        {
          "codeId": "a5G9p0000001HurEAE",
          "code": "Mr.",
          "crossRef": [
            {
              "systemCode": "BBS",
              "xrefValue": "MR"
            },
            {
              "systemCode": "SYS2",
              "xrefValue": "Mister"
            }
          ]
        }
      ]
    }
  ]
}

我期待输出为,

有效载荷=

{
"patient": {
"patientNumber": "123",
"secondaryAltPatientIdentifier": "2222222",
"title": "Mister",
"preferredName": "test"
}
}
mule dataweave mulesoft
2个回答
1
投票

您可以使用

update
运算符更改字段的值。然后使用
filter()
查找数组中符合条件的元素。我假设我的解决方案中始终存在有效匹配项。我首先通过
title
systemCode
过滤。

%dw 2.0
output application/json
var secondJson={
    "objectCode": "Title",
    "masterObjectValues": [
        {
            "systemCode": "MASTER",
            "systemObjectValues": [
                {
                    "codeId": "112",
                    "code": "Dr.",
                    "crossRef": [
                        {
                            "systemCode": "UAT",
                            "xrefValue": "DR"
                        },
                        {
                            "systemCode": "SYS2",
                            "xrefValue": "DR"
                        }
                    ]
                },
                {
                    "codeId": "113",
                    "code": "Mr.",
                    "crossRef": [
                        {
                            "systemCode": "UAT",
                            "xrefValue": "MR"
                        },
                        {
                            "systemCode": "SYS2",
                            "xrefValue": "Mister"
                        }
                    ]
                }
            ]
        }
    ]
}
---
payload update {
    case patient at .patient -> patient update {
        case title at .title -> ((secondJson.masterObjectValues[0].systemObjectValues filter ($.code == title)) [0].crossRef filter ($.systemCode=="SYS2"))[0].xrefValue 
    }
}

输出:

{
  "patient": {
    "patientNumber": "111",
    "secondaryAltPatientIdentifier": "2222222",
    "title": "Mister",
    "preferredName": "abc"
  }
}

0
投票

另一种改造方式

%dw 2.0
output application/json
var secondJson={
  "objectCode": "Title",
  "masterObjectValues": [
    {
      "systemCode": "MASTER",
      "systemObjectValues": [
        {
          "codeId": "a5G9p0000001HwTEAU",
          "code": "Dr.",
          "crossRef": [
            {
              "systemCode": "BBS",
              "xrefValue": "DR"
            },
            {
              "systemCode": "NAVSYD",
              "xrefValue": "DR"
            }
          ]
        },
        {
          "codeId": "a5G9p0000001HurEAE",
          "code": "Mr.",
          "crossRef": [
            {
              "systemCode": "BBS",
              "xrefValue": "MR"
            },
            {
              "systemCode": "SYS2",
              "xrefValue": "Mister"
            }
          ]
        }
      ]
    }
  ]
}
var y = secondJson.masterObjectValues.systemObjectValues[0] groupBy $.code mapObject ((value, key, index) -> 
    (key) : {(value.crossRef[0] map (
        {
            ($.systemCode) : $.xrefValue
        }
     ) )}
)
 
---
payload  update {
    case .patient.title -> y[$].SYS2
}
© www.soinside.com 2019 - 2024. All rights reserved.