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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • When One Giant Payload Must Serve Many Small Consumers: Designing a Scalable Fanout Service
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Automatic 1111: Custom Sketch-to-Image API
  • On-Demand-Schedulers With MuleSoft CloudHub APIs

Trending

  • Why Google Data Migration Gets Stuck at 99%: Causes and Proven Fixes
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • S3 Vectors: How to Build a RAG Without a Vector Database

Transforming Messages With DataWeave

DataWeave is an expression language designed by MuleSoft. The language helps in transforming the incoming payload to various payloads as per the requirements.

By 
Kapish Bhardwaj user avatar
Kapish Bhardwaj
·
Jul. 20, 21 · Analysis
Likes (2)
Comment
Save
Tweet
Share
7.3K Views

Join the DZone community and get the full member experience.

Join For Free

DataWeave is an expression language designed by MuleSoft. The language helps in transforming the incoming payload to various payloads as per the requirements.  Most developers write transformation scripts in the transforming message or set payload components. The language is tightly integrated with the Mule runtime engine and is a must-learn for every MuleSoft developer.

A few examples for transforming the input payload into various patterns:

Example 1: Given an array, we’ll generate the following output pattern using the input payload.

Input payload:

          [     {"name": "Roger"    },     {"name": "Michael"    },     {"name": "Harris"    }     ]


Required Output Pattern:

          [  {    "user 1": "Roger"  },   {    "user 2": "Micheal"  },   {    "user 3": "Harris"  }        ]


Expression Used to Transform:

%dw 2.0 output application/json --- payload.name map ((item, index) -> {   ("user" ++" "++ index+1): item })

 

Example 2: Given two arrays, [1,2,3,4,5,6,7] and [4,5,6,7,1], we’ll merge into a single array without duplicates, [1,2,3,4,5,6,7]:

Expression Used to Transform:

%dw 2.0 output application/json var str = [1,2,3,4,5,6,7] var str1= [4,5,6,7,1] --- (str ++ str1) distinctBy ((item, index) ->item )

 

Example 3: From the object, we’ll remove the “last name” and “first name” key-value pair.

Input Payload:

{        "first name": "Keith",       "last name": "Peters",        "age": 25        }


Required Output Pattern :

{ "age": 25
}


Expression Used to Transform:

%dw 2.0 output application/json --- payload filterObject ((value, key) -> (key as String != "first name") and key as String !="last name")

 

Example 4: From the array [1,2,3,’a’,’b’], remove 2 and b.

Expression Used to Transform:

%dw 2.0 output application/json var arr = [1,2,3,'a','b'] --- arr-2-'b'

 

Example 5: Given the below JSON, transform all names to uppercase.

Input Payload:

    [    {"name": "Roger"    },    {"name": "Michael"   },    {"name": "Harris"   }    ]


Output Pattern:

[ {   "Name": "ROGER" }, {   "Name": "MICHAEL" }, {   "Name": "HARRIS" } ]


Expression Used to Transform:

%dw 2.0 output application/json fun UpperCase(input_value)=upper(input_value) --- payload map(item, index) -> Name:UpperCase(item.name)

 

Example 6: Retrieve all the records of users with an age greater than 20.

Input Payload:

[ {  "name": "Chris Jordan",  "age": "19"
}, {  "name": "Glenn Maxwell",  "age": "24"
}, {  "name": "Mike",  "age": "20"
} ]


Expression Used to Transform:

%dw 2.0 output application/json --- payload filter ((item, index) -> item.age>20)

 

Example 7: Retrieve the average age of persons in the input payload.

Input Payload:

[ {  "name": "Chris Jordan",  "age": "19"
}, {  "name": "Glenn Maxwell",  "age": "24"
}, {  "name": "Mike",  "age": "20"
} ]


Expression Used to Transform:

%dw 2.0 output application/json --- payload filter ((item, index) -> item.age>20) 

 

Example 8: Sort persons in payload according to age.

Input Payload:

[ {  "name": "Chris Jordan",  "age": "19"
}, {  "name": "Glenn Maxwell",  "age": "24"
}, {  "name": "Mike",  "age": "20"
} ]


Expression Used to Transform:

%dw 2.0 output application/json --- payload orderBy ($.age)


Note: If we want the data in descending order

%dw 2.0 output application/json --- payload orderBy (-$.age)

 

Example 9: Skip the null values from the input payload.

Input Payload:

[ {  "name": "Chris Jordan",  "age": null
}, {  "name": "Glenn Maxwell",  "age": "24"
}, {  "name": "Lisa",  "age": null
} ]


Expression Used to Transform:

%dw 2.0 output application/json skipNullOn = 'everywhere' --- payload

 

Example 10: Extract the file name from the input payload which is after “/”

{   "filename":  "njnjnjnnjnbhbhjb/ui900jnnjn"
}


Expression Used to Transform:

%dw 2.0 output application/json import * from dw::core::Strings --- substringAfterLast(payload.filename,'/')

 

Example 11: Transform the present date and time to format like “yyyy-MM-dd HH:mm:ss.S”

Expression Used to Transform:

%dw 2.0 output application/json --- now() as LocalDateTime as String {format: "yyyy-MM-dd HH:mm:ss.S"} 


In all the above examples, we took the input payloads in JSON formats and tried to convert the inputs into the required patterns using DataWeave. Thanks for reading!

Payload (computing)

Opinions expressed by DZone contributors are their own.

Related

  • When One Giant Payload Must Serve Many Small Consumers: Designing a Scalable Fanout Service
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Automatic 1111: Custom Sketch-to-Image API
  • On-Demand-Schedulers With MuleSoft CloudHub APIs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook