Data Weave Solved Exercises in Mule 4 : Part-1
There were few interesting challenge found over page: Ace MuleSoft Interview: DataWeave Exercises in Mule 4 | MuleGuru and here I tried to solved and capture the solutions below:
Exercise 1:
Extract the initials from the Full Name.
Input
{
"fullName": "Nagaraju Kshathriya Raghunathrao"
}
Output
{
"Initial": "NKR"
}
Solution
%dw 2.0
output application/json
---
"Initial": (payload.fullName splitBy(" "))
map ((item, index) -> item[0])
reduce ((item, accumulator="") -> accumulator ++ item )
Exercise 2:
Convert an array of integers to an array of strings.
Input
[1, 2, 3, 4, 5]
Output
["1", "2", "3", "4", "5"]
Solution
%dw 2.0
output application/json
---
payload map ((item, index) -> item as String)
Exercise 3:
Transform a date string to a different date format.
Input
"2022-01-01"
Output
"01-Jan-2022"
Solution
%dw 2.0
output application/json
---
payload as Date {format: "yyyy-MM-dd"} as String {format: "dd-MMM-yyyy"}
Exercise 4:
Sort a list of strings in descending order.
Input
["apple", "banana", "orange", "grape"]
Output
["orange", "grape", "banana", "apple"]
Solution
%dw 2.0
output application/json
---
(payload orderBy ((item, index) -> item ))[-1 to 0]
Exercise 5:
Extract unique values from an array of strings.
Input
["2021", "1994", "2034", "2032", "2021", "2022", "1995", "2032"]
Output
["2021","1994","2034","2032","2022","1995"]
Solution
%dw 2.0
output application/json
---
payload distinctBy ((item, index) -> item )
Exercise 6:
Group a list of transactions by month and calculate the total amount for each month.
Input
[
{"date": "2022-01-05", "amount": 100},
{"date": "2022-01-15", "amount": 150},
{"date": "2022-02-10", "amount": 200},
{"date": "2022-02-25", "amount": 120}
]
Output
{
"01": {"total": 250},
"02": {"total": 320}
}
Solution
%dw 2.0
output application/json
import * from dw::core::Strings
var groupedPayload= payload groupBy ((item, index) -> item.date substringBeforeLast("-") substringAfterLast ("-") )
---
{
(groupedPayload pluck ((value, key, index) ->{
(key) : {
"total": sum(value.amount)
}
})
)
}
Exercise 7:
You have a list of temperatures in Celsius. Transform the data to Fahrenheit using the formula F = (C * 9/5) + 32
.
Input
{
"temperaturesInCelsius": [0, 10, 25, 30, -5]
}
Output
{
"temperaturesInFahrenheit": [32, 50, 77, 86, 23]
}
Solution
%dw 2.0
output application/json
fun temFahrenheit(temperature) = (temperature* (9/5)) + 32
---
{
"temperaturesInFahrenheit": payload.temperaturesInCelsius map ((item, index) -> temFahrenheit(item) )
}
Exercise 8:
You have a list of order amounts, and you need to calculate the total order amount.
Input
{
"orderAmounts": [120, 50, 75, 200, 100]
}
Output
{
"totalOrderAmount": 545
}
Solution
%dw 2.0
output application/json
import * from dw::core::Arrays
---
{
"totalOrderAmount" : payload.orderAmounts sumBy ((param_1) ->param_1 )
}
Exercise 9:
Extract unique words from a sentence.
Input
"Even if they are djinns, I will get djinns that can outdjinn them."
Output
["Even","if","they","are","djinns","I","will","get","that","can","outdjinn","them"]
Solution
%dw 2.0
output application/json
---
flatten(trim(payload replace (".") with "") splitBy(", ")
map ((item, index) -> item splitBy (" "))) distinctBy ((item, index) -> item)
Exercise 10:
Transform each person’s data to include the full name and birth year.
Input
[
{"name": "John", "age": 25},
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 22}
]
Output
[
{"fullName": "John", "birthYear": 1999},
{"fullName": "Alice", "birthYear": 1994},
{"fullName": "Bob", "birthYear": 2002}
]
Solution
%dw 2.0
output application/json
import * from dw::core::Dates
---
payload map ((item, index) ->{
"fullName": item.name,
"birthYear": ((now() as Date {format: "yyyy"}) as String {format: "yyyy"} as Number) - item.age as Number
} )
Exercise 11:
Combine two arrays of objects based on a common key.
Input-1
[
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
Input-2
[
{"id": 1, "age": 25},
{"id": 2, "age": 30}
]
Output
[
{"id": 1, "name": "Alice", "age": 25},
{"id": 2, "name": "Bob", "age": 30}
]
Solution
%dw 2.0
output application/json
var firstArray = [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
var secondArray = [
{"id": 1, "age": 25},
{"id": 2, "age": 30}
]
---
firstArray map ((itemMap, indexMap) ->{
"id": itemMap.id,
"name": itemMap.name,
"age": (secondArray filter ((itemFilter, indexFilter) -> itemFilter.id == itemMap.id )).age[0]
} )
Exercise 12:
Merge multiple JSON objects dynamically.
Input
[
{"name": "Alice"},
{"age": 25},
{"city": "New York"}
]
Output
{"name": "Alice", "age": 25, "city": "New York"}
Solution
%dw 2.0
output application/json
---
{(payload)}
Exercise 13:
Select dept on the basis of employee id matching in the list of ids under departments
Input
{
"employee": [
{
"id": 2,
"name": "Anand"
},
{
"id": 1,
"name": "Vivek"
},
{
"id": 3,
"name": "Ismail"
}
],
"departments": [
{
"dept": "dept1",
"ids": [
"1",
"3"
]
},
{
"dept": "dept2",
"ids": [
"2"
]
}
]
}
Output
[
{
"id": "1",
"name": "Vivek",
"dept": "dept1"
},
{
"id": "2",
"name": "Anand",
"dept": "dept2"
},
{
"id": "3",
"name": "Ismail",
"dept": "dept1"
}
]
Solution
%dw 2.0
output application/json
---
payload.employee map ((itemOuterMap, indexOuterMap) -> {
"id": itemOuterMap.id,
"name": itemOuterMap.name,
"dept": ((payload.departments filter ((itemFilter, indexFilter) -> itemFilter.ids contains itemOuterMap.id))
map ((item, index) -> item.dept ))[0]
} ) orderBy ((item, index) -> item.id )