Tring the below dataweave but its not able to achieve using pluck. Please help :
Input:
{ "EmpoyeeID" : "123" , "employee_salary_in_last3_months" : 1000 , "employee_salary_in_last6_months": 1750, "employee_salary_in_last12months" : 3000, }
Output:
{ "EmpoyeeID": "123", "Salary": { "Amounts": [ { "Duration": "3 months", "Salary": "1000" }, { "Duration": "6 months", "Salary": "1750" }, { "Duration": "12 months", "Salary": "3000" } ] } }
Tried the below function :
%dw 2.0
output application/json
---
{
"EmpoyeeID" : payload.EmpoyeeID,
"Salary": {
"Amounts": payload pluck ((value, key, index) ->
"Duration" : value.employee_salary_in_last3_months
)
}
}
You are using incorrectly pluck(). It returns an array of each key-pair in the input object. key
is the actual key and value
the actual value. For example for employee_salary_in_last3_months" : 1000
then key
is employee_salary_in_last3_months
(of type Key) and value
is 1000
. So it makes no sense to use a key selector on this value.
Also pluck() will return records for all key-pairs, not just the salary related ones. You can use filterObject()
to remove key EmpoyeeID
(should it be EmployeeID?). Additionally you need to extract the number of months from the key name. I used a couple of replace...with
.
%dw 2.0
output application/json
---
{
"EmpoyeeID": payload.EmpoyeeID,
"Salary": {
"Amounts":
payload
filterObject ((value, key) -> key as String startsWith "employee_salary_in_last")
pluck ((value, key) ->
{
"Duration": (key as String) replace "employee_salary_in_last" with "" replace "_" with "" replace "months" with " months",
Salary: value
}
)
}
}
Input (fixed):
{
"EmpoyeeID" : "123",
"employee_salary_in_last3_months" : 1000,
"employee_salary_in_last6_months": 1750,
"employee_salary_in_last12months" : 3000
}
Output:
{
"EmpoyeeID": "123",
"Salary": {
"Amounts": [
{
"Duration": "3 months",
"Salary": 1000
},
{
"Duration": "6 months",
"Salary": 1750
},
{
"Duration": "12 months",
"Salary": 3000
}
]
}
}