Posts by Hubert Klein Ikkink

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 →

DataWeave Delight: Wrapping string values

Posted on by  
Hubert Klein Ikkink

If we want to wrap a string value with another string value we can use the wrapWith and wrapIfMissing functions defined in the dw::core::Strings module. The first argument of these functions is the string we want to wrap and the second argument is the string value that wraps the first argument. The function wrapIfMissing will only apply the wrapper string if it was not already applied.

To remove a wrapped character from a wrapped string we can use the unwrap function. The first argument is the string value that is already wrapped and the second argument the character we want to use for unwrapping. The second argument can only be a single character, but we can repeatedly invoke the unwrap function to remove multiple wrap characters.

Continue reading →

DataWeave Delight: Finding highest or lowest value in an array

Posted on by  
Hubert Klein Ikkink

DataWeave has several function to find the highest or lowest value in an array. The items in the array need to be of the same type and implement the Comparable interface. To find the highest value we can use the max function. The function returns the maximum value based on the type of the item in the array. If we want to find the highest value based on other criteria we can use the maxBy function. This function takes a lambda as last argument where we can return a Comparable value that is used to determine the highest value. To find the lowest value in an array we have similar functions: min and minBy.

Continue reading →

shadow-left