Dataweave Series for Practice & Interview Part-2
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.
- Array to Object
Input:-
["a:b","c:d"]
Output:-
[
{
"a": "b"
},
{
"c": "d"
}
]
Dataweave Code:-
payload map{
(($ splitBy ":")[0]):($ splitBy ":")[1]
}
2. Search a field in the same payload using other field values
Input:-
[{
"ID": "A",
"name": "123",
"bossID": "C"
},
{
"ID": "B",
"name": "345",
"bossID": "D"
},
{
"ID": "C",
"name": "456",
"bossID": "B"
},
{
"ID": "D",
"name": "789",
"bossID": "C"
}]
Output:-
[{
"EmpName": "123",
"BossName": "456"
},
{
"EmpName": "345",
"BossName": "789"
},
{
"EmpName": "456",
"BossName": "345"
},
{
"EmpName": "789",
"BossName": "456"
}]
Dataweave Code:-
payload map(item, index)->{
"EmpName": item.name,
"BossName": (payload filter $.ID == item.bossID).name[0]
}
3. Group By Year present inside a String
Input:-
{
"AU": ["PROD-202100BG12",
"PROD-202100BG12",
"PROD-202000BG12",
"PROD-201900BG12"]
}
Output:-
{
"2021": [
"PROD-202100BG12",
"PROD-202100BG12"
],
"2020": [
"PROD-202000BG12"
],
"2019": [
"PROD-201900BG12"
]
}
Dataweave Code:-
import * from dw::core::Strings
- -
payload.AU groupBy ((item, index) -> substringAfter(item,"-")[0 to 3])
4. Output the Following Pattern
Input:-
{
"number": "1034101910291333"
}
Output:-
[
"1034",
"10341019",
"103410191029",
"1034101910291333"
]
Dataweave Code:-
var a=abs(sizeOf(payload.number)/4)
- -
0 to a-1 map payload.number[0 to 4*($+1)-1]
5. Filter based on field value and form a String
Input:-
[
{
"name": "abc",
"city" : "hyd"
},
{
"name": "xyz",
"city" : "delhi"
},
{
"name": "abc1",
"city" : "hyd"
}
]
Output:-
"abc,abc1"
Dataweave Code:-
((payload filter $.city=="hyd").name) joinBy(',')
6. Round down Value to Nearest Whole Number
Input:-
"14.99"
Output:-
14
Dataweave Code:-
floor("14.99")
7. Print ASCII/UniCode value of Character & Vice-Versa
Input:-
{
"ASCII Value":"A",
"Char at ASCII":"65"
}
Output:-
{
"ASCII Value": 65,
"Char at ASCII": "A"
}
Dataweave Code:-
import * from dw::core::Strings
- -
{
"ASCII Value":charCode(payload."ASCII Value"),
"Char at ASCII":fromCharCode(payload."Char at ASCII" as Number)
}
8. Filter Key & Values of Objects in Separate Array
Input:-
{
"firstName": "EmpFirstName",
"lastName": "EmpLastName",
"address": "EmpAddress"
}
Output:-
{
"Keys": [
"firstName",
"lastName",
"address"
],
"Values": [
"EmpFirstName",
"EmpLastName",
"EmpAddress"
]
}
Dataweave Code:-
{
"Keys":keysOf(payload),
"Values":valuesOf(payload)
}
9. Filter files based on File extension
Input:-
["a.csv","b.csv","c.xlsx","d.docx","e.docx"]
Output:-
["a.csv","b.csv","c.xlsx"]
Dataweave Code:-
import * from dw::core::Strings
- -
payload filter ( $ endsWith ("csv")) or ( $ endsWith ("xlsx"))
10. Filter Values present in both array
Input:-
{
"a" : ["12", "15", "17"] ,
"b":["15", "18", "20", "12" , "89"]
}
Output:-
["12","15"]
Dataweave Code:-
payload.a filter (payload.b contains $)
📢 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/