Reading Time: 7 minutes

We all know how powerful Dataweave Transform Message component is. This is such a powerful template engine that allows us to transform data to and from any format (XML, CSV, JSON, Pojos, Maps, etc. basically ).

So if we need to transform we need a Dataweave component in our flow. But wait! Dataweave also provides us a function called Dataweave function that helps us to execute Dataweave language outside a Dataweave transform component. To execute a Dataweave expression outside a Dataweave transform component, we need to put the expression inside dw() function wrapped inside within an MEL expression #[ ].

latest report
Learn why we are the Leaders in API management and iPaaS

In this blog, we will demonstrate the use of Dataweave expression outside a Dataweave  Transform Message component by using dw() function.

Use case example

Let’s take a use case where we have a JSON payload that needs to be transformed into an XML or a JSON payload of our format.  The JSON payload will enter in our Mule application as an input and will get transformed into a modified JSON payload/ XML  payload as output.

First, we will see the simple example of converting an input JSON into our JSON format as follows:-

1

Now with a Dataweave component we can easily transform it into our required JSON  by designing the Dataweave script as follows:-

However, what about using the powerful Dataweave expression outside a Dataweave transform component?

We will try to achieve this without using a Dataweave Transform Message component.

We will be creating a Mule flow that will contain an HTTP listener endpoint to accept the input JSON payload followed by an Expression transformer component  that will execute our dw() function as below:-

1

So now our expression that contains dw() function  that will transform  the input JSON payload into  a JSON payload of our format is as follows:-

1

If we look closely, we will find the dw() function  is in the format:-  #[dw(“<Dataweave script>”, “Output Mime type”)]

The first parameter in this function is the Dataweave script that we use in our Dataweave component and the second parameter is the output type of the payload which we currently designed as application/json.

So, we have designed our Mule flow without a Dataweave component for transforming the payload yet using the same Dataweave expression language in the dw() function.

Simple, isn’t it?

The complete code will be as follows:-

Testing our flow:-

So if we run our application now and test the flow using a REST Client tool or a Postman and hit the URL http://localhost:8081/transform with POST method as follows:-

1

Here we will get our desired transformed JSON response as output.

Transforming to XML:-

To get our response in an XML format instead of a JSON format, we just need to change the output type as application/XML as follows:-

1
1

Testing our flow again:-

Now we will test again, and this time to check the output as XML. We hit the same URL http://localhost:8081/transform with POST method as follows:-

1

This is what we get above. A response in XML!

Conclusion with Dataweave expression example:-

As we can see the Dataweave language is such a powerful tools and can fit into an expression outside the Dataweave Transform Message component, and we just need to invoke the dw() function. The expression will return whatever the transform outputs is and in the output format defined within the parameter.

The expression can be put into any transformer component like Set Payload transformer or Expression transformer component. Using a dw() function, we can quickly query data and use it to log information from payloads, route data to another flow(using lookup function), or use it for quick payload transformation.

Dataweave  expressions defined within this function work just as those defined within a Dataweave  Transform Message element but must be enclosed in “quotation marks.”

Now it’s your turn to experiment with the dw() function. Please share your comments and experiences below in the comments section!

If you’re still new to Dataweave, check out our Getting started with Dataweave blog series.