Dataweave Series for Practice & Interview Part-4

Shubham Chaurasia
2 min readJul 24, 2022

--

Practice Dataweave here :- Dataweave Playground

Important Note:- First Copy Input, Output, DW Code & Use Json Online Validator, to format the JSON, since medium changes the JSON.

  1. Find the Object Inside an array with max/greatest value

Input:-

[{
"Name": "Shubham",
"Marks":"95"
}, {
"Name": "Shubh",
"Marks":"65"
}, {
"Name": "SC",
"Marks":"70"
}]

Output:-

{
"Name": "Shubham",
"Marks": "95"
}

Dataweave Code:-

payload maxBy((item)->item.Marks)

2. Difference between “not” & “!” operator

not operator works on the result of overall logical operation but ! works on the first result of logical operation

{
"x":true,
"y":false
}

Output:-

{
"r1": true,
"r2": false
}

Dataweave Code:-

{
"r1":not payload.x and payload.y==true,
"r2":!payload.x and payload.y==true
}

3. XML DateTime to Date Format (Tough One)

Input:-

<placedDate>2022-05-25 00:00:00.0</placedDate>

Output:-

"05/25/2022"

Dataweave Code:-

%dw 2.0
output application/json
var x=(payload.placedDate splitBy(' '))[0]
---
x as Date {format: "yyyy-MM-dd"} as String {format: "MM/dd/yyyy"}

4. Sort an Array

Input:-

[1.1,3,9,1.0]

Output:-

[1.0,1.1,3,9]

Dataweave Code:-

payload orderBy $

5. Extract Number from Escaped Characters

Input:-

"\"999\""

Output:-

999

Dataweave Code:-

payload replace ("\"") with ""

(payload replace /[^a-z0–9A-Z]/ with "") as Number

6. Mask/Hide Values in Dataweave

Input:-

{
"CreditCard":"4567–1234–9087"
}

Output:-

{
"CreditCard": "****-****-****"
}

Dataweave Code:-

%dw 2.0
output application/json
import * from dw::util::Values
- -
payload mask field("CreditCard") with "****-****-****"

7. Extract the URI Parameter/Value from a URL

Input:-

{ 
"url":"https://xyz.com/api/Emp/999"
}

Output:-

"999"

Dataweave Code:-

%dw 2.0
import * from dw::core::Arrays
output application/json
- -
slice( payload.url splitBy(/[\/]/), 5,6 )[0]

8. Assign Array Key to Each Array Value

Input:-

{
"Date" : [
"01/05/1991",
"31/01/1995"
]
}

Output:-

{
"Date": "31/01/1995",
"Date": "01/05/1991"
}

Dataweave Code:-

payload.Date map ((item, index) -> 
"Date":item
)reduce ($++$$)

9. Break Objects into SubObjects with specified number of key-value pairs

Input:-

{
"Name": "Shubham",
"Marks": 90,
"Name": "AShish",
"Marks": 50,
"Name": "Divya",
"Marks": 80
}

Output:-

[
{
"Name": "Shubham",
"Marks": 90
},
{
"Name": "AShish",
"Marks": 50
},
{
"Name": "Divya",
"Marks": 80
}
]

Dataweave Code:-

%dw 2.0
output application/json
import divideBy from dw::core::Objects
- -
payload divideBy 2

10. Underscore Space, Dash & Camel-Casing in a String

Input:-

"casualPlaces-required Yes"

Output:-

"casual_places_required_yes"

Dataweave Code:-

%dw 2.0
import * from dw::core::Strings
- -
underscore(payload)

📢 Check the other Articles in the Dataweave Series:- Dataweave Series for Practice & Interview Part 1–10

Thank You for sticking with the article until the end. If you found this helpful, please leave a Clap & Follow for more such amazing articles in the future.

Also Checkout More Amazing Articles:- Click Here

About Me:-

I am a MuleSoft Certified Developer & Architect working in Billennium. You can read more about me here. Follow me for Amazing Blogs & Articles.

Feel Free to Drop your queries on my below Communication Channels

Gmail:-myid535@gmail.com
LinkedIn:- https://www.linkedin.com/in/shubhamchaurasia1/

--

--

Shubham Chaurasia

MuleSoft Ambassador | Mule Certified Architect & Developer | 11x Salesforce| 4x AWS | 2x GCP | 2x Solace | 2xAzure | https://linkedin.com/in/shubhamchaurasia1/