The DW core functions are primarily used to transform data. These functions are imported automatically into the mule project and can be used in components like transform, set payload, and many others.
In this article, we will see some of the most commonly used core functions with examples.
The concatenation operator, ++, is used in the following scenarios
This operator throws an error if any one of the values is null or tries to concatenate incompatible data types.
The following DW code concatenates two strings "hello" and "world".
%dw 2.0 output application/json --- "hello" ++ " world"
This one produces the output "hello world".
We can concatenate two JSON objects with the ++ operator as shown in the below code.
%dw 2.0 output application/json --- { "name" : "bigdataers"} ++ {"country" : "united states"}
This one concatenates two objects and generates the below output.
{ "name": "bigdataers", "country": "united states" }Array Concatenation
Not only JSON objects, but we can also concatenate two JSON arrays as shown in the below code.
%dw 2.0 output application/json --- [1,2,3] ++ [4,5,6]
The result of this JSON array concatenation is [1,2,3,4,5,6]
The -- operator is used to remove values from the input values. It supports the following types of removals.
Let us see each of these operations in detail with examples.
In the below example, the values 2 and 4 are removed from the source and result in the array [1,3]
%dw 2.0 output application/json --- [1,2,2,3,4] -- [2,4]
To remove the key-value pairs from an object, we have to specify all these key-value pairs and use the -- operator as shown below.
%dw 2.0 output application/json --- {"a" : 1, "b" : 2, "c" : 3} -- {"b":2}
In the above example, the key-value pair "b": 2 is removed from the source object and results in {"a" : 1, "c" : 3}
There is another way to remove the key-value pairs from an object. That is specifying only the keys in an array. This is shown in the following code.
%dw 2.0 output application/json --- {"a" : 1, "b" : 2, "c" : 3} -- ["a"]
This DW code removes the key-value pair of "a" and keeps the "b" and "c" keys.
The contains operator is used to checking for the existence of a value in another value. It works in the following scenarios.
The following dataweave code checks for the existence of the string "mule" in another string "mulesoft".
%dw 2.0 output application/json --- { a : "mulesoft" contains "mule", b : "hello world" contains "user" }
The execution of the above DW code will result in the below output.
{ "a": true, "b": false }
Another use case of contains operator is to check for the presence of a value in an array. The following DW code illustrates this behavior.
%dw 2.0 output application/json --- { a : [1,2,3] contains 5, b : [1,2,3] contains 2 }
This DW code will produce the following output.
{ "a": false, "b": true }
The contains function returns only true/false. It does not return the position of the value. The find function returns all the matching indices in an array. In case there is no match, then the find function returns an empty array.
The below dataweave code finds the string "o" in the main string "hello world" and returns the matching indices.
%dw 2.0 output application/json --- "hello world" find "o"
This will return the array [4,7] upon execution.
We can use the find function to search for a value in an array and return all the matching indices. This is shown in the following example.
%dw 2.0 output application/json --- [1, 2, 3, 4, 2, 5] find 2
This DW code will return the indices [1,4].
We have covered some of the DW core functions in Mulesoft here. In another article, I will cover a few more functions in detail. Hope this article will help you to some extent.