Handling transformation of arrays in DataWeave 2.0
- November 30, 2020
When working with collections, use the map function to apply a transformation to each element in an array. The input array can be JSON or Java, and it returns an array of elements. A map function iterates to each element of an array and passes it to the lambda. A lambda is an anonymous function not bound to identity and shown as a pair of curly braces or an empty object.
There are two ways to denote it. The first way places the first parameter (input array in this case) to the left of the map function while the second parameter is committed, and in this case, usually placed on the right. The second way includes the second parameter to the right.
Here’s an example of the first way of denoting. In this example, the input payload is an array of objects with two properties.
- The first transformation passes the input payload to the map function and creates an output array of objects, each of which has three properties.
- The second transformation creates an object with a single property and uses the map function to assign the output arrays as the assigned value. The output array is the same structure as the input array, but with a different property name.
- Both transformations use “$” to refer value and “$$” to refer to the index or keys.
And here’s the same example showing the second way of denotation. In this case, we use the mapper function as the second parameter. It basically uses explicit arguments in lamdas for more readable code. It also allows you to name the object and the index. When using the second argument, you must include a return arrow, which is a hyphen followed by a right-angle bracket.
Using the mapper produces more readable code. If you have nested arrays, you must use this technique to differentiate between the outer and inner arrays. For example, you can use names such as objectOut or indexOut for the outer array and objectIn or indexIn for the inner array.
The transformations in the following example are identical to the previous one, except that they use “object” and “index” in place of the “$” and “$$” signs.
This is how we can transform the array data structure in DataWeave 2.0 (Mule 4).
Transforming Complex XML Data Structures
The XML data structure doesn’t support arrays. So, when transforming from a Java or JSON data structure to XML, it is mandatory to wrap the map operation in curly braces and parenthesis — that is, {(…)}.
- Curly braces “{}” define the object
- parentheses “( )” transform each element of the array into a key-value pair
In the following example, the first transformation fails because the map function tries to produce an array in XML format. The second transformation wraps the map function in curly braces and parentheses, which assigns an object of key-value pairs as the value of “users” property and displays them as child elements.
You can create a better XML structure by nesting each input element of an array in an output child element, called “user,” as shown below.
When the input is a complex XML structure, DataWeave can easily convert it to JSON or Java.
NOTE: Use .* selector to reference repeated elements.
In the example above:
- The first transformation expression uses “payload” to automatically transform XML elements to JSON object structure. Notice that the attributes are ignored by default.
- The second transformation uses the root element’s name to retrieve the child element.
- The third transformation uses the child element’s name to retrieve the information. Notice that only the first child element is returned.
- In the fourth transformation, the “.*” notation is used to receive all child elements and it creates an array output. So, we understand how to create an array from an XML input here.
- In the last transformation, notice that the “@” notation is used to access the attribute, which is by default ignored.
When testing the application with Postman, Advance REST Client or other similar applications, you may notice that you’re able to see only the last element in the responses. If that’s the case, switch from “pretty” mode to “raw” mode, and you’ll be able to view the full response.
— By Sanket Kangle