1. Objects (dw::core::Objects)
DataWeave 2.0 Object related functions are: divideBy, entrySet, mergeWith, nameSet & valueSet.
-
- divideBy:
Divides object into sub-objects with properties.
dwl script:
{a: 1, b: true, a:2, b:false} divideBy 2
Output:
[
{
"a": 123,
"b": true
},
{
"a": 123,
"b": false
}
]
-
- entrySet:
Returns list of key-value pair objects.
dwl script:
Objects::entrySet({a: true, b: 1})
Output:
[
{
key: "a",
value: true,
attributes: null
},
{
key: "b",
value: 1,
attributes: null
}
]
-
- mergeWith:
Keeps the target object as constant, then appends to that with the source key-value pairs.
dwl script:
{a: false, b: 2} mergeWith {a: true, c: "Hello"}
Output:
{"a": true, "b": 2 , "c": "Hello"}
-
- nameSet:
Returns list of key names from an object.
dwl script:
Objects::nameSet({a: true, b: 1})
Output:
["a", "b"]
-
- valueSet:
Returns list of key values of an object.
dwl script:
Objects::valueSet({a: true, b: 1})
Output:
[true,1]
2. Runtime (dw::Runtime)
Runtime related functions are fail, failif & wait ….. These should be imported into DataWeave code by adding import dw::Runtime at header.
-
- fail:
Throws an exception with specified message.
dwl script:
%dw 2.0
import dw::Runtime
---
Runtime::fail("Timeout Error")
Output:
Timeout Error
-
- failif:
Returns an exception with the specified message if defined expression evaluates true. If not, returns the value.
dwl script:
%dw 2.0
import failIf from dw::Runtime
output application/json
---
{ "a" : "b" } failIf ("b" is String)
Output:
Failed
-
- wait:
Stops execution for the specified timeout.
dwl script:
%dw 2.0
import * from dw::Runtime
output application/json
---
{user: 1} wait 2000
Output:
{
"user": 1
}
3. Strings (dw::core::Strings)
String related functions are camelize, capitalize, dasherize, pluralize & underscore … etc.
-
- camelize:
a: camelize(“murali_thuraka”) will gives : “a” : “muraliThuraka”
-
- capitalize:
a: capitalize(“murali_thuraka”) will gives : “a” : “MuraliThuraka”
-
- dasherize:
a: dasherize(“Murali Thuraka”) will gives : “a” : “Murali-Thuraka”
-
- pluralize:
a: pluralize(“box”) will gives : “a” : “boxes”
-
- underscore:
a: underscore(“Murali Thuraka”) will gives : “a” : “Murali_Thuraka”
4. System (dw::System)
System-related functions are: envVar & envVars.
-
- envVar:
Returns an environment variable with a specified name.
dwl script:
%dw 2.0
import dw::System
output application/json
---
System::envVar("SYS_PSWD")
-
- envVars:
Returns all environment variables defined in the hosted system.
dwl script:
%dw 2.0
import dw::System
output application/json
---
System::envVars().SYS_PSWD
5. URL (dw::core::URL)
URL related functions are: compose, decodeURI & parseURI ….etc
-
- compose:
It is used to replace URL components by encodeURIcomponent result of it.
dwl script:
%dw 2.0
import * from dw::core::URL
output application/json
---
{ 'composition': compose `encoding http://asd/$(' Welcome to Mule - 4 ')/text now` }
Output:
{
"composition": "encoding http://asd/%20Welcome%20to%20Mule - 4%20/text now"
}
-
- decodeURI:
It decodes a Uniform Resource Identifier (URI) means that Replaces each escape sequence in the encoded URI with the character that it represents.
dwl script:
%dw 2.0
import * from dw::core::URL
output application/json
---
{
a: decodeURI('http://asd/%20text%20to%20decode%20/text')
}
Output:
{
"a": "http://asd/ text to decode /text"
}
-
- parseURI:
Parses an URL and returns a URI object.
dwl script:
%dw 2.0
import * from dw::core::URL
output application/json
---
{
'composition': parseURI('https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#footer')
}
Output:
{
"composition": {
"isValid": true,
"raw": "https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#footer",
"host": "en.wikipedia.org",
"authority": "en.wikipedia.org",
"fragment": "footer",
"path": "/wiki/Uniform_Resource_Identifier",
"scheme": "https",
"isAbsolute": true,
"isOpaque": false
}
}
For Mule 4 DataWeave Functions: Part 1
Are you still having issues with Mule 4 DataWeave functions? You can find more information in our MuleSoft blog. Our MuleSoft implementation and integration experts are here to help you. Contact us today.