I am running %dataweave 1.0 . I need to create a function which should replace some code values according to a certain payload which is received from an API call in my Mule flow. The sample API output is given below: This is stored in a session variable currently.
{
"CodeMaster": {
"PrimaryCodes": {
"PrimarySpecCodes": {
"ABC": {
"code": "Alpha Bravo Charlie",
"target": "SALES",
"Field": "PrimarySpecCodes"
},
"TUV": {
"code": "Tango Umbrella Victor",
"targetSystemCode": "SALES",
"targetCodeFieldName": "PrimarySpecCodes"
},
"XYZ": {
"code": "X-Ray Yankee Zulu",
"targetSystemCode": "SALES",
"targetCodeFieldName": "PrimarySpecCodes"
}
}
}
}
}
As shown above, I need to create a function which will replace the codes (like ABC, TUV, XYZ) in my main payload with the values "Alpha Bravo Charlie", "Tango Umbrella Victor" and "X-Ray Yankee Zulu" respectively. In the main payload, I have the data to be replaced like below:
"PY123":
{
"Country": "GB",
"Status": "ACTIVE",
"Flag": null,
"SpecCodes": [
{
"PrimarySpecCodes": "ABC"
},
{
"PrimarySpecCodes": "TUV"
},
{
"PrimarySpecCodes": "XYZ"
}
]
}
How can I create a function to replace the code values. If there is a better solution to replace codes, please suggest. Thanks in advance.
%dw 1.0
%output application/json
%function buildLookup(codes)
codes mapObject {($$): $.code}
%var codeLookup = buildLookup(sessionVars.code.CodeMaster.PrimaryCodes.PrimarySpecCodes)
%var verboseCodes = payload.PY123.SpecCodes map (code) ->
code mapObject {($$): codeLookup[$]}
---
{
"PY123" : {
"Country" : "GB",
"Status" : "ACTIVE",
"Flag" : null,
"SpecCodes" : verboseCodes
}
}