Deep into Dataweave - Mule 4
1) Use vars as an ArrayList:
Define the variable as an Array List using the below blank array (Use Set Variable activity):
%dw 2.0
output application/java
---
[]
Add values to the existing variable Array List (Use another Set Variable activity):
%dw 2.0
output application/java
---
vars.varList
+ payload
Lookup values from the variable ArrayList using the index:
vars.varList[2]
Full flow mule xml:
<flow name="varsAsArrayList" doc:id="ad2a593a-e7cb-4cd6-a46a-3e87d30f95f8"
>
<set-payload value='#[%dw 2.0
output application/java
---
["First","Second","Third"]]' doc:name="Set
Payload" doc:id="3ec00784-0453-44ce-baa9-71dc20ebbe1d"
/>
<set-variable value="#[%dw 2.0
output application/java
---
[]]" doc:name="varList"
doc:id="497a414b-b53c-4523-8b9a-9c4d5eeeda80"
variableName="varList" />
<foreach doc:name="For Each" doc:id="a8ea8c36-8055-4a98-bd98-db62d7f4697d"
collection="#[payload]"
>
<set-variable value="#[%dw 2.0
output application/java
---
vars.varList + payload]" doc:name="varList"
doc:id="83ce95be-d6eb-4472-9edf-41f5f0aa510f"
variableName="varList"/>
</foreach>
<logger level="INFO" doc:name="Logger"
doc:id="bf81f0ac-e630-48a0-b00c-adf3cdb8bb9d"
message="OP - #[vars.varList[2]]"
/>
</flow>
2) Use vars as a Map:
Define the variable as a Map using the below expression (Use Set Variable activity):
%dw 2.0
output application/java
---
{}
Add values to the existing variable Map (Use another Set Variable activity):
%dw 2.0
import mergeWith from dw::core::Objects
output application/java
---
vars.varMap mergeWith { ('key'): payload }
OR
%dw 2.0
output application/java
---
vars.varMap ++ { ('key'): payload }
Lookup values from the variable Map using the key:
syntax : #[vars.varMap['key']]
e.g: #[vars.varMap['2']] (2 is the key here)
e.g: #[vars.varMap['2']] (2 is the key here)
Full flow mule xml:
<flow name="varsAsMap">
<set-payload value='#[%dw 2.0
output application/java
---
["First","Second","Third"]]' doc:name="Set
Payload" doc:id="ff22bb3b-bb0c-4336-9081-7db5aac860e1"
/>
<set-variable value="#[%dw 2.0
output application/java
---
{}]" doc:name="varMap"
doc:id="db60044d-f6e4-4efa-b10d-e3992592e2d3"
variableName="varMap"/>
<foreach doc:name="For Each" doc:id="73a42901-31bd-42ef-b338-44fa815e5cd1"
collection="#[payload]">
<set-variable value="#[%dw 2.0
import mergeWith from
dw::core::Objects
output application/java
---
vars.varMap mergeWith { (vars.counter): payload }]" doc:name="varMap"
doc:id="24338e8d-1f3b-4caa-a6ba-5b98bb4c7bac"
variableName="varMap" />
</foreach>
<logger level="INFO" doc:name="Logger"
doc:id="99688a40-b3e3-4a10-a6dd-d773f79d28bb"
message="OP - #[vars.varMap['2']]"/>
</flow>
3) dw cheat codes:
a) Replace all special chars in a string:
"Hello how are you!##" replace /[^A-Za-z0-9]/ with "_"
4) Multiple Namespaces using DataWeave:
Expected Output -
<?xml version='1.0' encoding='UTF-8'?>
<pp:Root xmlns:pp="http://www.abc.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abc.org/data/Sample1.0.0.xsd">
<pp:Header>
<pp:Element>Hello NameSpace</pp:Element>
</pp:Header>
</pp:Root>
<pp:Root xmlns:pp="http://www.abc.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abc.org/data/Sample1.0.0.xsd">
<pp:Header>
<pp:Element>Hello NameSpace</pp:Element>
</pp:Header>
</pp:Root>
output application/xml
ns pp http://www.abc.org
var xmlnsxsi = "http://www.w3.org/2001/XMLSchema-instance"
var xsiSchemaLocation = "http://www.abc.org/data/Sample1.0.0.xsd"
---
{
pp#Root @("xmlns:xsi": xmlnsxsi, "xsi:schemaLocation": xsiSchemaLocation): {
pp#Header: {
pp#Element: "Hello NameSpace"
}
}
}
Comments
Post a Comment