Weaving it with Dataweave expression

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 (, 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 #[ ].

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 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.


 


We'd love to hear your opinion on this post


4 Responses to “Weaving it with Dataweave expression”

  1. Hi, Thanks for the nice blog.
    What is the advantage writing in Dw() function in expression fileter instead of using Data weave!
    Can i use this dw function in Java Component if Yes please let me know how to use that.

    Agree(0)Disagree(0)Comment
    • Hi Ganesh,
      As I have mentioned at the end of the blog that the advantage of dw() is to perform the features of Dataweave, so it can be used for quick payload transformation or to log information from payloads.
      for example you can use this function in a logger to check the value of an payload element here :- #[dw("payload.id")]

      Regarding using this expression in Java code, yes off course you can use it, but for that you need to evaluate the expression in Java using ExpressionManager() of MuleContext

      Agree(4)Disagree(0)Comment
      • Hi ,
        It is very use full post. Thanks for the information. I have one doubt, Mule developed on Java, to transform json to xml or anything in dataweave internally which technologies they used. is it groovy ? please let me know.
        Thanks in advance.

        Agree(0)Disagree(0)Comment