Hi Team,
i'm seeking help with DWL. I want to create an array of arrays, where each array will have multiple objects and sum of all these objects "Active_Contracts_Count" should be less than 100.
Which mean's each array will multiple objects and if i take the sum of these objects "Active_Contracts_Count" that should be less than 100.
sample input:
[
{
"Org_Id" : "abc1234-efg1234",
"Org_Name" : "SAAS",
"Env_Id" : "env99-en32",
"Env_Name" : "SAAS-QA",
"Api_Id" : "13444",
"Active_Contracts_Count" : 18
},
{
"Org_Id" : "abc1234-efg1234",
"Org_Name" : "SAAS",
"Env_Id" : "4344-en32",
"Env_Name" : "SAAS-DEV",
"Api_Id" : "18829",
"Active_Contracts_Count" : 7
},
{
"Org_Id" : "abc1234-efg1234",
"Org_Name" : "SAAS",
"Env_Id" : "ss-e21-sd",
"Env_Name" : "SAAS-CICD",
"Api_Id" : "28943",
"Active_Contracts_Count" : 55
},
{
"Org_Id" : "abc1234-efg1234",
"Org_Name" : "SAAS",
"Env_Id" : "4344-en32",
"Env_Name" : "SAAS-DEV",
"Api_Id" : "35765",
"Active_Contracts_Count" : 8
},
{
"Org_Id" : "abc1234-efg1234",
"Org_Name" : "SAAS",
"Env_Id" : "4344-en32",
"Env_Name" : "SAAS-DEV",
"Api_Id" : "40349",
"Active_Contracts_Count" : 19
},
{
"Org_Id" : "abc1234-efg1234",
"Org_Name" : "SAAS",
"Env_Id" : "env99-en32",
"Env_Name" : "SAAS-QA",
"Api_Id" : "40382",
"Active_Contracts_Count" : 98
}
]
expected output
[
[
{
"Org_Id": "abc1234-efg1234",
"Org_Name": "SAAS",
"Env_Id": "env99-en32",
"Env_Name": "SAAS-QA",
"Api_Id": "13444",
"Active_Contracts_Count": 18
},
{
"Org_Id": "abc1234-efg1234",
"Org_Name": "SAAS",
"Env_Id": "4344-en32",
"Env_Name": "SAAS-DEV",
"Api_Id": "18829",
"Active_Contracts_Count": 7
},
{
"Org_Id": "abc1234-efg1234",
"Org_Name": "SAAS",
"Env_Id": "ss-e21-sd",
"Env_Name": "SAAS-CICD",
"Api_Id": "28943",
"Active_Contracts_Count": 55
},
{
"Org_Id": "abc1234-efg1234",
"Org_Name": "SAAS",
"Env_Id": "4344-en32",
"Env_Name": "SAAS-DEV",
"Api_Id": "35765",
"Active_Contracts_Count": 8
}
],
[
{
"Org_Id": "abc1234-efg1234",
"Org_Name": "SAAS",
"Env_Id": "env99-en32",
"Env_Name": "SAAS-QA",
"Api_Id": "40382",
"Active_Contracts_Count": 98
}
],
{
"Org_Id": "abc1234-efg1234",
"Org_Name": "SAAS",
"Env_Id": "4344-en32",
"Env_Name": "SAAS-DEV",
"Api_Id": "40349",
"Active_Contracts_Count": 19
}
]
Anurag Sharma (Nagarro) Forum Ambassador
Hello @Rajat Raj
try this
%dw 2.0
output application/json
fun sumOfLastArray(arr) =
if ((arr != null) and (sizeOf(arr) > 0) and (typeOf(arr[-1]) ~= "Array"))
((arr[-1] reduce ((item, total = 0) -> total + item.Active_Contracts_Count)))
else 0
---
payload reduce (e, acc = []) ->
if (isEmpty(acc) or ((sumOfLastArray(acc) + e.Active_Contracts_Count) > 100))
(acc ++ [[e]])
else acc[0 to -2] default [] ++ [(acc[-1] ++ [e])]