DataWeave

DataWeave Delight: Splitting An Array Or Object

Posted on by  
Hubert Klein Ikkink

The module dw::core::Arrays has extra functions that are useful when working with arrays in DataWeave. In this post we will look at the functions splitAt and splitWhere. We can use these functions to split an array into two arrays. The result of both functions is actually a Pair type, which is defined as an object with the keys l and r. An example is { "l": 1, "r": 2 } which we can read as the left side of the pair has value 1 and the right side of the pair has value 2. The result of the splitAt and splitWhere function will have one part of the split array in the left side of the pair and the rest of the array in the right side.

Continue reading →

DataWeave Delight: Importing Functions With An Alias

Posted on by  
Hubert Klein Ikkink

When we want to reference functions from other modules than the dw::Core module we must us the import statement. We can use the as keyword to map a function name that we want to import to another name that we want to use in our DataWeave code. We provide an alias for the function that we can use in our code. This could be useful if we think our code gets more readable in a certain context by using different function names, but still want to use the original function implementation. Next to providing an alias for function names, we can also provide an alias for a module name, when we use the import <ModuleName> as <NewModuleName> statement. In our DataWeave code we can then refer to the new alias to call functions from the module.

Continue reading →

DataWeave Delight: Reversing the items in an array

Posted on by  
Hubert Klein Ikkink

To reverse the order of the items in the array we have two possible solutions in DataWeave. The first solution uses the index operator to access items in an array ([…​]). We can define a range as value for the index operator to get all elements that have a index value that is part of the range. For example we could use [1 to 3] to get the second up to the fourth items from the array. The value -1 can be used to indicate the last index in the array, so we could get a new array where the items are in reversed order by using the range [-1 to 0].

Continue reading →

DataWeave Delight: Convert string value to a boolean

Posted on by  
Hubert Klein Ikkink

If we need to convert a string value "true" or "false" to a boolean we can use the toBoolean function from the dw::util::Coercions module. The function will return a boolean true if the string value is "true", mixed casing is allowed. And the function returns false for a mixed casing string value of "false". Any other string value will throw an exception and will not return a boolean value.

Continue reading →

DataWeave Delight: Turn string value into array of characters

Posted on by  
Hubert Klein Ikkink

A string value can be seen as an array of characters and if we want to transform our string value to an array we can use the toArray function in the dw::util::Coercions module. Once we have transformed our string to an array we can use all functions that work on arrays. The nice thing about DataWeave is that some functions that work on arrays already have an overloaded version that accepts a string value. Then we don’t have to explicitly use the toArray function, but we can simply use our original value when we invoke the function.

Continue reading →

DataWeave Delight: Using dynamic elements to build a new object

Posted on by  
Hubert Klein Ikkink

When we create an object structure it is very useful to use dynamic elements. Dynamic elements can turn an object or array of objects into key and value pairs in an object constructor. The syntax of the dynamic element is that an object or array of objects is enclosed in parentheses ((…​)). We use this inside an object constructor enclosed in curly braces ({…​}) so we get the key/value pairs. The objects can be referenced by a variable or a function that returns an object.

Continue reading →

DataWeave Delight: Using pluck function to transform object to array

Posted on by  
Hubert Klein Ikkink

Sometimes we get a data structure with keys and values and we want to create an array with data from the data structure. DataWeave gives us the pluck function to achieve this. The input argument is an object and a lambda and the output must be an array. We use a lambda to define the mapping from our object to elements in the resulting array. The lambda has three arguments: the value for a key, the key itself and the index of the key in the object. We have a handle on all data to pluck the information and transform it to elements in an array.

Continue reading →

DataWeave Delight: Using dynamic elements to build a new object

Posted on by  
Hubert Klein Ikkink

When we create an object structure it is very useful to use dynamic elements. Dynamic elements can turn an object or array of objects into key and value pairs in an object constructor. The syntax of the dynamic element is that an object or array of objects is enclosed in parentheses ((…​)). We use this inside an object constructor enclosed in curly braces ({…​}) so we get the key/value pairs. The objects can be referenced by a variable or a function that returns an object.

Continue reading →

DataWeave Delight: Turn string value into array of characters

Posted on by  
Hubert Klein Ikkink

A string value can be seen as an array of characters and if we want to transform our string value to an array we can use the toArray function in the dw::util::Coercions module. Once we have transformed our string to an array we can use all functions that work on arrays. The nice thing about DataWeave is that some functions that work on arrays already have an overloaded version that accepts a string value. Then we don’t have to explicitly use the toArray function, but we can simply use our original value when we invoke the function.

Continue reading →

DataWeave Delight: Using string interpolation

Posted on by  
Hubert Klein Ikkink

In DataWeave we can use expressions in strings that will be evaluated and inserted into the string value. This is called string interpolation. The expression must be enclosed in parentheses where the first parenthesis is prefixed with a dollar sign: $(<expression>). The expression must return a string value or can be automatically coerced into a string value in order for it to work. The expression can also be a variable.

Continue reading →

shadow-left