Table of Contents
DataWeave is a powerful transformation language that is used to transform data from one format to another. It is a core component of MuleSoft Anypoint Platform.
This script will convert an XML payload to a JSON payload. It will force keys to be arrays if there is only one element with that key since duplicateKeyAsArray
only works when there are multiple elements with the same key.
Input XML Payload:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<About>
<Code>29329573000145</Code>
<Name>COMPANY X</Name>
<CodeStatus>Ativa</CodeStatus>
</About>
<Emails>
<Email>[email protected]</Email>
</Emails>
<Emails>
<Email>[email protected]</Email>
</Emails>
<Phones>
<AreaCode>19</AreaCode>
<Phone>34140000</Phone>
</Phones>
<Phones>
<AreaCode>19</AreaCode>
<Phone>34370005</Phone>
</Phones>
<Mobiles>
<AreaCode>11</AreaCode>
<Phone>912341234</Phone>
</Mobiles>
<Mobiles>
<AreaCode>11</AreaCode>
<Phone>956785678</Phone>
</Mobiles>
</Root>
DataWeave Script:
%dw 2.0
output application/json
var root = 'Root'
var arrayKeys = ["Emails", "Phones", "Mobiles"]
var objectKeys = keysOf( (payload.'$(root)' default {}) as Object) -- (arrayKeys)
---
{
(objectKeys map {
(($): ((payload.'$(root)'.'$($)' default {}) as Object mapObject ((value, key, index) ->
(key): (value match {
case is String -> (
if(trim(value) ~= "NULL") null else trim(value)
)
else -> value
})
)))
}),
(
arrayKeys map {
(
($): (
(payload.'$(root)'.*'$($)' default []) as Array map ((item, index) ->
item as Object mapObject ((value, key, index) ->
(key): (value match {
case is String -> (
if(trim(value) ~= "NULL") null else trim(value)
)
else -> value
})
)
)
)
) if(payload.'$(root)'.'$($)'?)
})
}
Output JSON Payload:
{
"About": {
"Code": "29329573000145",
"Name": "COMPANY X",
"CodeStatus": "Ativa"
},
"Emails": [
{
"Email": "[email protected]"
},
{
"Email": "[email protected]"
}
],
"Phones": [
{
"AreaCode": "19",
"Phone": "34140000"
},
{
"AreaCode": "19",
"Phone": "34370005"
}
],
"Mobiles": [
{
"AreaCode": "11",
"Phone": "912341234"
},
{
"AreaCode": "11",
"Phone": "956785678"
}
]
}
This script will mask sensitive data in the XML text. It will use the logger.dataMaskingFieldsCSV
property to define the fields that will be masked.
Input XML Payload:
<?xml version="1.0" encoding="WINDOWS-1252"?>
<Root>
<Participant>
<AccountNumber>376400</AccountNumber>
<Name>JOHN</Name>
<PhoneNumber>+5500123451234</PhoneNumber>
<Email></Email>
<Id>1</Id>
<DocumentNumber>00000000000</DocumentNumber>
</Participant>
</Root>
DataWeave Script:
%dw 2.0
output application/json
fun xmlTextMaskSensitiveData(xml, fields) = do {
(flatten(
fields map ((field, idx) ->
xml scan ("(\<$(field)\>)(.+)(\<\/$(field)\>)" as Regex) map ((found, index) ->
if(!isEmpty(found[2]))
{
o: "$(field)>"++ found[2] ++"</$(field)",
r: "$(field)>***</$(field)",
}
else
null
)
)
) filter !isEmpty($) )
reduce ((texto, x = xml) ->
x
replace (texto.o as String)
with (texto.r as String)
)
}
---
{
original: payload.XML,
masked: xmlTextMaskSensitiveData(payload.'XML', (Mule::p("logger.dataMaskingFieldsCSV") splitBy ",") )
}
Output JSON Payload:
{
"original": "<?xml version=\"1.0\" encoding=\"WINDOWS-1252\"?><Root><Participant><AccountNumber>376400</AccountNumber><Name>JOHN</Name><PhoneNumber>+5500123451234</PhoneNumber><Email></Email><Id>1</Id><DocumentNumber>00000000000</DocumentNumber></Participant></Root>",
"masked": "<?xml version=\"1.0\" encoding=\"WINDOWS-1252\"?><Root><Participant><AccountNumber>376400</AccountNumber><Name>JOHN</Name><PhoneNumber>***</PhoneNumber><Email></Email><Id>1</Id><DocumentNumber>***</DocumentNumber></Participant></Root>"
}