Using the DataWeave exercise below, I'll demonstrate how to use the reduce, map, valuesOf, splitBy functions, format, and ++ operator.
Given Input:
JSON
[
{
"empId": "ex12345",
"fullName": "Peter parker",
"street": "Second Ave 46",
"city": "San Francisco",
"state": "veniam",
"postal": "sit",
"country": "Mexico",
"dept": "hr",
"joinedDate": "2021-12-22",
"miles": 68
},
{
"empId": "ex54321",
"fullName": "Joseph charles",
"street": "First Ave 47",
"city": "Bangalore",
"state": "Karnataka",
"postal": "560100",
"country": "India",
"dept": "finance",
"joinedDate": "2021-11-15",
"miles": 49
}
]
Desired Output:
JSON
[
{
"firstName": "Peter",
"lastName": "parker",
"AddrWithValuesOf": "San Francisco,veniam,sit,Mexico",
"AddrWithPluck": "San Francisco,veniam,sit,Mexico",
"miles": 68.00,
"DateofJoin": "22-Dec-2021"
},
{
"firstName": "Joseph",
"lastName": "charles",
"AddrWithValuesOf": "Bangalore,Karnataka,560100,India",
"AddrWithPluck": "Bangalore,Karnataka,560100,India",
"miles": 49.00,
"DateofJoin": "15-Nov-2021"
}
]
We can accomplish the desired output by writing the following DataWeave.