Dataweave Series for Practice & Interview

Priya Shaw
4 min readJul 1, 2023

--

➤ Trim Only the End Spaces, and skip trimming a particular Key

Explanation/Logic:- In the following input, skip trimming the ‘Code’ Key and also trim only the end spaces. In the first object only Billed and Salary needs to be trimmed. In the second object, only Age and Salary need to be trimmed

Input:-

{
"Employee": [
{
"Code": " 501-AKASH",
"Age": "26",
"Salary": "900000 ",
"Date": "03/04/2022",
"Billed": "NO "
},
{
"Code": " 502-PRIYA",
"Age": "30 ",
"Salary": "1000000 ",
"Date": "22/02/2021",
"Billed": "YES"
}
]
}

Output:-

{
"Employee": [
{
"Code": " 501-AKASH",
"Age": "26",
"Salary": " 900000",
"Date": "03/04/2022",
"Billed": "NO "
},
{
"Code": " 502-PRIYA",
"Age": "30",
"Salary": " 1000000",
"Date": "22/02/2021",
"Billed": "YES "
}
]
}

Dataweave Code:-

%dw 2.0
output application/json
fun trimEndSpace(val)= if(val[-1]=="") trimEndSpace(val[0 to -2]) else val
fun trimAllValues(obj)=
obj mapObject {
(if ('$$' == "Code") (($$): $) else ($$): trimEndSpace($))
}
---
"Employee": payload.Employee map ((item, index) ->
trimAllValues(item)
)

➤ Print the following Input to Output pattern

Explanation/Logic:- In the output, no of “tasks” objects depend on no of unique Ids in input i.e., (if I have Id AB, AC, AC, AC, AB )= Tasks in output is 2 tasks(AB/AC) each for one unique id.
and “lineItems” array always 2 objects only but “charges” array objects again depends on no of ABs /AC in input
Please ignore the values I have added dummy values the number of objects in the array of charges and the number of tasks objects is important

Input:-

{
"results": [{
"id": "AB"
}, {
"id": "AC"
}, {
"id": "AC"
},
{
"id": "AC"
},
{
"id": "AB"
}
]
}

Output:-

[{
"AB": {
"lineItems": [{
"no": "1",
"charges": [{
"AB": "Delivery"
},
{
"AB": "Delivery"
}
]
},
{
"no": "2",
"charges": [{
"AB": "Delivery"
},
{
"AB": "Delivery"
}
]
}
]
}
},
{
"AC": {
"lineItems": [{
"no": "1",
"charges": [{
"AC": "Delivery"
},
{
"AC": "Delivery"
},
{
"AC": "Delivery"
}
]
},
{
"no": "2",
"charges": [{
"AC": "Delivery"
},
{
"AC": "Delivery"
},
{
"AC": "Delivery"
}
]
}
]
}
}
]

Dataweave Code:-

%dw 2.0
output application/json
fun y(val)=payload.results reduce ((item, acc=0) -> if (item.id~=val) acc + 1 else acc)
---
payload.results distinctBy $ map
{
($.id):{
"lineItems":[
{
"no":"1",
"charges": (0 to y($.id)-1) reduce ((item, acc=[{}]) -> if (item !=4)acc+ {
($.id):"Delivery"} else acc ) filter $!={}
},
{
"no":"2",
"charges": (0 to y($.id)-1) reduce ((item, acc=[{}]) -> if (item !=4)acc+ {
($.id):"Delivery"} else acc ) filter $!={}
}
]
}
}

➤ Keys should come dynamic in response, if any key is present more than once you need to append its value in response. E.g., address is present twice, there its value add-1 & add-2 are added in final response

Input:-

[{
"key1": "address",
"value": "add-1"
},
{
"key1": "address",
"value": "add-2"
},
{
"key1": "postal",
"value": "208021"
}
]

Output:-

[
{
"address": "add-1 add-2"
},
{
"postal": "208021"
}
]

Dataweave Code:-

%dw 2.0
import * from dw::core::Objects
output application/json
---
valueSet(payload groupBy ((item, index) -> item.key1)) map ((group, index1) ->{
(group[0].key1):flatten(group.value) joinBy " "
} )

Note:- Use ValuesOf in place of valueSet for dw 2.3 & above

➤ Business is parent of industry, industry is parent of businessLine and so on. There are chances that one of the levels might not be present. such as after industry directly facility may come.

Input:-

[{
"business": {
"id": "rel",
"desc": "rel industry",
"industry": {
"id": "manufacture",
"desc": "manufacturing",
"businessLine": {
"id": "residential",
"desc": "residential manufacture",
"facility": {
"id": "mumbai",
"desc": "mumbai location"
}
}
}
}
}]

Output:-

{
"asset": [
{
"key": "rel",
"desc": "rel industry",
"parent": "business"
},
{
"key": "manufacture",
"desc": "manufacturing",
"parent": "industry"
},
{
"key": "residential",
"desc": "residential manufacture",
"parent": "businessLine"
},
{
"key": "mumbai",
"desc": "mumbai location",
"parent": "facility"
}
]
}

Dataweave Code:-

%dw 2.0
output application/json
var temp={}
fun rec(value)= value mapObject ((val, key, index) ->if (typeOf(val)~="Object") {"parent":(key) } ++ rec(val) else {(key):(val)})
var pattern=[rec(payload[0])]
var id=pattern.*id
var desc=pattern.*desc
var parent=pattern.*parent
---
{
"asset":id map ((item, index) -> {
"key":item,
"desc":desc[index],
"parent":parent[index]
}
)
}

Feel Free to Drop your queries on my below Communication Channels

Gmail:- priyashaw0411@gmail.com
LinkedIn:- https://www.linkedin.com/in/priya-shaw/

If you found this helpful, please leave a Clap & Follow for more such amazing articles in the future.

Also Checkout the Other Articles:- Click Here

--

--

Priya Shaw
Priya Shaw

Written by Priya Shaw

Sr Integration Specialist @LTIMindtree | MuleSoft Mentor | IBM WMB/IIB/ACE/CP4I | IBM MQ | IBM APIC | 1x MuleSoft | 3x Azure | 1x OCI | Java | DevOps