I had asked a similar question yesterday and got a correct answer, but today I faced more issues. So here is the new problem statement. I am fetching customer contacts from salesforce which are coming as an array of objects as shown below.
Input Payload:
[
{
"customerID": 1,
"customerName": "Jonhn1"
},
{
"customerID": 1,
"customerName": "Jonhn2"
},
{
"customerID": 1,
"customerName": "Jonhn3"
},
{
"customerID": 1,
"customerName": "Jonhn4"
},
{
"customerID": 1,
"customerName": "Jonhn5"
},
{
"customerID": 2,
"customerName": "Jonhn6"
},
{
"customerID": 2,
"customerName": "Jonhn7"
},
{
"customerID": 2,
"customerName": "Jonhn8"
},
{
"customerID": 3,
"customerName": "Jonhn9"
},
{
"customerID": 3,
"customerName": "Jonhn10"
},
{
"customerID": 3,
"customerName": "Jonhn11"
},
{
"customerID": 4,
"customerName": "Jonhn12"
},
{
"customerID": 4,
"customerName": "Jonhn13"
},
{
"customerID": 5,
"customerName": "Jonhn14"
},
{
"customerID": 5,
"customerName": "Jonhn15"
},
{
"customerID": 5,
"customerName": "Jonhn16"
},
{
"customerID": 6,
"customerName": "Jonhn17"
},
{
"customerID": 7,
"customerName": "Jonhn17"
}
]
I need the output to be an array of arrays, each sub-array should have all the customer details of at most three different customers. I got the solution till the above statement as show below
%dw 2.0
output application/json
import * from dw::core::Arrays
---
payload groupBy $.customerID pluck $ divideBy 3 map((flatten($)))
Now the main problem, We have to put a limit on the number of customers in an array. For example in the given input payload customer with id "1" came more than 4 (four) times, then this customer should be in a different array ( means a separate array of customer "1" e.g an array for five records) this can happen to any other customer as well.
So we have to check if a customer is repeating more than 4 times then make a separate array for that type of customer else combine it with other customers as required earlier
Expected output:
[
[
{
"customerID": 1,
"customerName": "Jonhn1"
},
{
"customerID": 1,
"customerName": "Jonhn2"
},
{
"customerID": 1,
"customerName": "Jonhn3"
},
{
"customerID": 1,
"customerName": "Jonhn4"
},
{
"customerID": 1,
"customerName": "Jonhn5"
}
],
[
{
"customerID": 2,
"customerName": "Jonhn6"
},
{
"customerID": 2,
"customerName": "Jonhn7"
},
{
"customerID": 2,
"customerName": "Jonhn8"
},
{
"customerID": 3,
"customerName": "Jonhn9"
},
{
"customerID": 3,
"customerName": "Jonhn10"
},
{
"customerID": 3,
"customerName": "Jonhn11"
},
{
"customerID": 4,
"customerName": "Jonhn12"
},
{
"customerID": 4,
"customerName": "Jonhn13"
}
],
[
{
"customerID": 5,
"customerName": "Jonhn14"
},
{
"customerID": 5,
"customerName": "Jonhn15"
},
{
"customerID": 5,
"customerName": "Jonhn16"
},
{
"customerID": 6,
"customerName": "Jonhn17"
},
{
"customerID": 7,
"customerName": "Jonhn18"
}
]
]
Please verify all the scenarios and correct me if anything I missed. I just seggregated based on the size of individual array.
If size[array] > 4 then divideBy
5 else divideBy
3.
distinctBy
to maintain a single payload as I have used payload in if and else condition
Note -> Order will not be maintained as you mentioned for rainy scenarios.
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = payload groupBy $.customerID pluck $
---
flatten(a map
(if (sizeOf($)>4)
$ divideBy sizeOf($) map(flatten($))
else
(a filter (sizeOf ($)< 5) divideBy 3 map (flatten ($)))
) distinctBy $)
Output
[
[
{
"customerID": 1,
"customerName": "Jonhn1"
},
{
"customerID": 1,
"customerName": "Jonhn2"
},
{
"customerID": 1,
"customerName": "Jonhn3"
},
{
"customerID": 1,
"customerName": "Jonhn4"
},
{
"customerID": 1,
"customerName": "Jonhn5"
}
],
[
{
"customerID": 2,
"customerName": "Jonhn6"
},
{
"customerID": 2,
"customerName": "Jonhn7"
},
{
"customerID": 2,
"customerName": "Jonhn8"
},
{
"customerID": 2,
"customerName": "Jonhn9"
},
{
"customerID": 3,
"customerName": "Jonhn10"
},
{
"customerID": 3,
"customerName": "Jonhn11"
},
{
"customerID": 4,
"customerName": "Jonhn12"
},
{
"customerID": 4,
"customerName": "Jonhn13"
}
],
[
{
"customerID": 5,
"customerName": "Jonhn14"
},
{
"customerID": 5,
"customerName": "Jonhn15"
},
{
"customerID": 5,
"customerName": "Jonhn16"
},
{
"customerID": 6,
"customerName": "Jonhn17"
},
{
"customerID": 7,
"customerName": "Jonhn17"
}
]
]