I'm trying to filter an array based on some values nested in objects.
My data pertains to customers with outstanding balances on their accounts. I have the age of the balance nested in buckets from 10-29 and 31-.
I want to keep any customers with an account balance over $1000. I also want to keep customers with an account balance of over $200 aged over 30 days. These accounts will get flagged.
From the example, customer 50001 should get removed from the payload, with 50002 and 50003 remaining:
{
"data": {
"araging": [
{
"customerid": "50001",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "59.2"
},
{
"agingperiod": "31-",
"totalamount": "50.00"
}
]
},
{
"customerid": "50002",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "0.00"
},
{
"agingperiod": "31-",
"totalamount": "246.00"
}
]
},
{
"customerid": "50003",
"aging": [
{
"agingperiod": "10-29",
"totalamount": "1084.60"
},
{
"agingperiod": "31-",
"totalamount": "0.00"
}
]
}
]
}
}
}
My first attempt is to get the customers over $200 for over 30 days.
%dw 2.0
output application/json
---
{
arAccounts: payload.data.*araging filter ($.aging.totalamount as Number > 200 and $.aging.agingperiod == "31-") map (araging, index) ->
{
customerid: araging.customerid,
totalamount: araging.aging.totalamount as Number
}
}
That code keeps giving messages about Arrays not being comparable to single values, and I'm unclear on how to iterate through each value of "totalamount." Changing type from String to Number isn't working, since it's an array value. And I need to know how I could sum the values, since any account over $1000 will be flagged. A bonus would be to add the flag field within this same transform.
Thanks in advance.