Dataweave Series for Practice & Interview Part-5

Shubham Chaurasia
4 min readJul 29, 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. Dynamic Value of Key in Key-Value Pair

Input:-

[
{
"Source": " customerName",
"destination": " customer "
},
{
"Source": " customerAddress",
"destination": " address"
}
]

Output:-

{
" customerName": " customer ",
" customerAddress": " address"
}

Dataweave Code:-

payload map ((item, index) -> 
(item.Source):item.destination
) reduce($$++$)

2. Filter department and count

Input:-

[
{
"firstName" : "John",
"lastName" : "Doe",
"jobTitle" : "Engineer",
"department": "Technology"
},
{
"firstName" : "Smith",
"lastName" : "Doe",
"jobTitle" : "Engineer",
"department": "Technology"
},
{
"firstName" : "Kelly",
"lastName" : "Doe",
"jobTitle" : "Analyst",
"department": "Operations"
},
{
"firstName" : "Keith",
"lastName" : "Doe",
"jobTitle" : "Analyst",
"department": "Finance"
}
]

Output:-

[
{
"department": "Technology",
"count": 2
},
{
"department": "Operations",
"count": 1
},
{
"department": "Finance",
"count": 1
}
]

Dataweave Code:-

%dw 2.0
import * from dw::core::Arrays
output application/json
---
payload distinctBy($.department)map(item,index)->{
"department": item.department,
"count":payload countBy ($.department==item.department )
}

3. Print the alphabets with their count of occurrence in the array

Input:-

["a","b","c","a","b","a"]

Output:-

["a3","b2","c1"]

Dataweave Code:-

%dw 2.0
import * from dw::core::Arrays
output application/json
---
payload distinctBy $ map ((item,index)-> item ++ (payload countBy($==item)))

4. Write If Condition without Else

Input:-

{
"Name": "Shubham",
"Age": "26"
}

Output:-

{
"Name": "Shubham"
}

Dataweave Code:-

{
"Name":[(payload.Name) if payload.Age>25][0]
}

Explanation:- This works only if you wrap the value inside the parenthesis and inside the array.

5. Trim Values inside nested JSON Structure

Input:-

{
"Employee": [
{
"Code": " 501-AKASH",
"Age": "26",
"Salary": [{
"Base":" 330000 "
}],
"Date": {
"Joining":" 26/10/2020 "
},
"Billed": [" NO "]
},
{
"Code": " 502-PRIYA",
"Age": "30 ",
"Salary": [{
"Base":"200000 "
}],
"Date": {
"Joining":" 26/10/2022 "
},
"Billed": [" YES "]
}
]
}

Output:-

{
"Employee": [
{
"Code": "501-AKASH",
"Age": "26",
"Salary": [
{
"Base": "330000"
}
],
"Date": {
"Joining": "26/10/2020"
},
"Billed": [
"NO"
]
},
{
"Code": "502-PRIYA",
"Age": "30",
"Salary": [
{
"Base": "200000"
}
],
"Date": {
"Joining": "26/10/2022"
},
"Billed": [
"YES"
]
}
]
}

Dataweave Code:-

%dw 2.0
output application/json
fun trimVal(value) = value match {
case is String -> trim($)
case is Array -> value map (trimVal($))
case is Object -> value mapObject {
($$):trimVal($)
}
else -> $
}
---
trimVal(payload)

6. Separate Multiple Values with Single Key into Different Objects

Input:-

{
"NameList": "Shubh,Priya"
}

Output:-

%dw 2.0
output application/json
var valuesList=payload.NameList splitBy ','
---
valuesList map ((item, index) ->
{
(("Student ") ++(index)): item
}
)

Dataweave Code:-

[
{
"Student 0": "Shubh"
},
{
"Student 1": "Priya"
}
]

7. Usage of FilterTree Function

Input:-

{
"firstName": "",
"lastName": "Shubham",
"friends": [
{
"id": "",
"test": {
"val":""
}
},
{
"age": "123"
},
""
]
}

Output:-

{
"lastName": "Shubham",
"friends": [
{
"test": {

}
},
{
"age": "123"
}
]
}

Dataweave Code:-

%dw 2.0
import * from dw::util::Tree
output application/json
---
payload filterTree ((value, path) ->
value match {
case s is String -> !isEmpty(s)
else -> true
})

8. Capitalize the leaf values of nested object: Usage of mapLeafValues function

Input:-

{
"user": [{
"name": "Shubham",
"lastName": {
"Middle":"",
"Last":"Chaurasia"
}
}],
"group": "dataweave series"
}

Output:-

{
"user": [
{
"name": "SHUBHAM",
"lastName": {
"Middle": "",
"Last": "CHAURASIA"
}
}
],
"group": "DATAWEAVE SERIES"
}

Dataweave Code:-

%dw 2.0
import * from dw::util::Tree
output application/json
---
payload mapLeafValues (value, path) -> upper(value)

9. Print Last Date of the Month given a date as input

Input:-

{
"lastday": "10-02-2023"
}

Output:-

{
"lastDay": 28
}

Dataweave Code:-

%dw 2.0
fun lastDay(date) =
(((date as Date {format: "dd-MM-yyyy"} as String {format: "01-" ++ "MM-yyyy"}) as Date {format: "dd-MM-yyyy"}) + |P1M| - |P1D|).day
output application/json
---
{
"lastDay": lastDay(payload.lastday)
}

Another Solution with Library

%dw 2.0
import * from dw::core::Dates
output application/json
---
{
"lastDay": atBeginningOfMonth(payload.lastday as Date {format: "dd-MM-yyyy"} + |P1M|) - |P1D|
}

10. Add/Update values in objects from other array objects based on matching ID field

Input:-

{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"products": [
{
"id": "5001",
"type": "None"
},
{
"id": "5002",
"type": "Glazed"
},
{
"id": "5005",
"type": "Sugar"
},
{
"id": "5007",
"type": "Powdered Sugar"
},
{
"id": "5006",
"type": "Chocolate with Sprinkles"
},
{
"id": "5003",
"type": "Chocolate"
},
{
"id": "5004",
"type": "Maple"
}
],
"price": [
{
"id": "5001",
"cost": 2
},
{
"id": "5005",
"cost": 5
},
{
"id": "5007",
"cost": ""
},
{
"id": "5004",
"cost": 6
},
{
"id": "5008",
"cost": ""
}
]
}

Output:-

[
{
"id": "5001",
"type": "None",
"price": 2
},
{
"id": "5002",
"type": "Glazed",
"price": null
},
{
"id": "5005",
"type": "Sugar",
"price": 5
},
{
"id": "5007",
"type": "Powdered Sugar",
"price": ""
},
{
"id": "5006",
"type": "Chocolate with Sprinkles",
"price": null
},
{
"id": "5003",
"type": "Chocolate",
"price": null
},
{
"id": "5004",
"type": "Maple",
"price": 6
}
]

Dataweave Code:-

payload.products map ((item, index) -> {
"id":item.id,
"type":item."type",
"price":(payload.price filter $.id == item.id).cost[0]
}
)

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

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/