Over a million developers have joined DZone.

Weaving It With Dataweave Expression

The Dataweave language is a powerful tool and can fit into an expression outside the Dataweave Transform Message component. We just need to invoke the dw() function.

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

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

Say 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 a 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 the 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 or XML  payload as output.

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

Image title

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

<dw:transform-message doc:name="Transform Message">
<dw:input-payload />
 <dw:set-payload><![CDATA[
  %dw 1.0
  %output application/json
  ---
 {
  "root": {
    "myId": payload.id,
    "myName": payload.name,
    "mydescription": payload.description
          }
 }
 ]]></dw:set-payload>
</dw:transform-message>

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:

Image title

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:

Image title

#[dw("{'root':{'myId':payload.id,'myName':payload.name,'mydescription':payload.description}}","application/json")]

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:

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>

<flow name="DataweaveExpressionFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/transform" doc:name="HTTP"/>
<expression-transformer doc:name="Expression" expression="#[dw(" {'root':{'myId':payload.id,'myName':payload.name,'mydescription':payload.description}}","application/json")]" />
</flow>

Testing Our Flow

We run our application and test the flow using a REST Client tool or a Postman and hit this URL with a POST method as follows:

Image title

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

Image title

#[dw("{'root':{'myId':payload.id,'myName':payload.name,'mydescription':payload.description}}","application/xml")]

Image title

Testing Our Flow Again

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

Image title

This is what we get. A response in XML!

Conclusion With Dataweave Expression Example

As we can see, the Dataweave language is a powerful tool and can fit into an expression outside the Dataweave Transform Message component. 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 the 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 the 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!

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
json ,dataweave ,integration ,weaving

Published at DZone with permission of Anirban Sen Chowdhary, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}