I want to update the payload.title value with the comparing the second JSON. In below payload.title value 'Mr.'
should be updated with secondJson xrefValue
value of 'Mister'
. The comparing condition is payload.value should equal to secondJson.masterObjectValues[0].code
, then the payload.title value should update with xrefValue value where systemCode
= SYS2
.
Could you please help me to sort this using data weave language?
payload =
{
"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"
}
]
}
]
}
]
}
I'm expecting the output as,
payload =
{
"patient": {
"patientNumber": "123",
"secondaryAltPatientIdentifier": "2222222",
"title": "Mister",
"preferredName": "test"
}
}
You can use the update
operator to change the value of a field. Then use filter()
to find the elements in the arrays that match a condition. I'm assuming that there is always a valid match in my solution. I filter first by the title
and the 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
}
}
Output:
{
"patient": {
"patientNumber": "111",
"secondaryAltPatientIdentifier": "2222222",
"title": "Mister",
"preferredName": "abc"
}
}