Handling Transformation of Arrays in DataWeave 2.0

Author: Sanket Kangle

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 is shown as a pair of curly braces or an empty object. 

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. There are two ways to denote it. The first way is that the first parameter (input array in this case) is placed left of the map function and the second parameter is committed in this case which is usually placed on the right. The second way is to include the second parameter to the right as well.

Let us see the example of the first way of denoting.

In the following 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 having 3 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 but with a different property name.
  • Both transformations use “$” to refer value and “$$” to refer to the index or keys.

Now, the same example in the second way of denotation. Where we use the mapper function as the second parameter. It’s basically using explicit arguments in lamdas for more readable code. It allows you to name the object and index. When the second argument is used, you must include a return arrow which is a hyphen followed by a right angle bracket. 

Using the mapper produces more readable code and 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, indexOut for the outer array, and objectIn, indexIn for the inner array.

The transformations in the below example are identical to the previous one. They just use “object” and “index” in place of “$” 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 Java or JSON data structure to XML, it is mandatory to wrap the map operation in curly braces and parenthesis i.e. {(…)}. 

  • Curly braces “{}” defines the object
  • parentheses “( )” are used to transform each element of the array into key-value pair 

In the following exhibit, 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, so they are displayed as child elements. 

We can create a better XML structure by nesting each input element of an array in an output child element called “user”. Below is the exhibit for the same.

When the input is a complex XML structure, DataWeave can easily convert it into JSON or Java.

(Note: Use .* selector to reference repeated elements)

In the exhibit 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 elements’ name to retrieve the child element.
  • The third transformation uses the child elements’ name to retrieve the information. Notice only the first child element is returned.
  •  In the fourth transformation, “.*” notation is used to receive all the 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 applications like the postman or advance rest client, you may notice that you are able to see only the last element in the responses. If that is the case, switch from “pretty” mode to “raw” mode and you will be able to view the full response. 

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.