Dataweave Series for Practice & Interview Part-3
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.
- Filter Duplicate Names from a String
Input:-
{
"name": "abhi bhim cat dog cat enton abhi fom"
}
Output:-
["abhi","bhim","cat","dog","enton","fom"]
Dataweave Code:-
(payload.name splitBy(' ')) distinctBy $
2. Remove Fields(first name and last name) from an Array
{
"fname":"a",
"lname":"b",
"age":"23"
}
Output:-
{
"age": "23"
}
Dataweave Code:-
payload - "fname" - "lname"
3. Assign Incremental Key to fields in an array
Input:-
[{
"name": "a"
},
{
"name": "b"
}
]
Output:-
[
{
"user1": "a"
},
{
"user2": "b"
}
]
Dataweave Code:-
payload map ((item, index) -> (
item mapObject(v,k,i)->{
("user" ++ index+1):v
}
)
)
4. Filter out blank fields from an array
Input:-
{
"arrivalDateTime": "",
"cabin": {},
"fareComponent": [],
"flightNumber": "3234",
}
Output:-
{
"flightNumber": "3234"
}
Dataweave Code:-
payload filterObject ((value, key, index) -> sizeOf(value)!=0)
5. Convert Current Data & Time to a Specific Time Zone
Input:-
Output:-
"2022–06–22T16:42:09.980593+05:30"
Dataweave Code:-
now() >> "IST"
6. Find out Available Time Zones for Date Time Conversion
Input:-
Output:-
Dataweave Code:-
java!java::util::TimeZone::getAvailableIDs()
7. Difference between ~= and ==
Input:-
Output:-
{
"eq1": false,
"eq2": true,
"eq3": false,
"eq4": true,
"eq5": false,
"eq6": true
}
Dataweave Code:-
{
"eq1": "true"==true,
"eq2": "true"~=true,
"eq3": "789"==789,
"eq4": "789"~=789,
"eq5": ["true"]==[true],
"eq6": ["true"]~=[true]
}
8. Extract Numbers from a AlphaNumeric String
Input:-
{
"number": "Hello612wor9ld!"
}
Output:-
["6","1","2","9"]
Dataweave Code:-
(payload.number splitBy ('')) filter (isNumeric($)==true)
9. Combine multiple objects into single object
Input:-
[
{
"id1":"101",
"name1":"shubham"
},
{
"id2":"102",
"name2":"mule"
}
]
Output:-
{
"id1": "101",
"name1": "shubham",
"id2": "102",
"name2": "mule"
}
Dataweave Code:-
payload reduce ($$ ++ $)
10. Merge Two Arrays and remove duplicates
Input:-
{
"arr1": [1,2,3,4],
"arr2": [2,5,6,4]
}
Output:-
[1,2,3,4,5,6]
Dataweave Code:-
(payload.arr1 ++ payload.arr2) distinctBy $
📢 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/