DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Weaving It With Dataweave Expression

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.

Anirban Sen Chowdhary user avatar by
Anirban Sen Chowdhary
·
Nov. 22, 16 · Tutorial
Like (4)
Save
Tweet
Share
8.97K Views

Join the DZone community and get the full member experience.

Join For Free

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!

JSON Payload (computing)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Debugging Streams and Collections
  • Apache NiFi Overview
  • Real-Time Stream Processing With Hazelcast and StreamNative
  • Building Angular Library and Publishing in npmjs Registry

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: