-1

I'm looking for an output similar to this one below where i want to groupBy costomer and orderid.

Input:

[ { "item": 621, "orderid": "ON22", "qty": 45.0, "customer": "610", "date": "1988-08-13" }, { "item": 63, "orderid": "ON2234", "qty": 7, "customer": "813", "date": "2001-08-13" } ]

Desired output: [ { "customer":"813", "data":[ { "item":63, "qty":7, "orderid":"ON2234", "date":"2001-08-13" } ] }, { "customer":"610", "data":[ { "item": 621, "qty": 45.0, "orderid": "ON22", "date": "1988-08-13" } ] } ]

james11
  • 55
  • 6

1 Answers1

1

You can simply map the default output of your groupBy result since your output does not require any additional logic.

%dw 2.0
output application/json
---
payload groupBy $.customer pluck ((customerOrders, customerId) -> {
    customer: customerId as String,
    data: customerOrders
})
Harshank Bansal
  • 2,798
  • 2
  • 7
  • 22
  • if i have this input : [ { "item": 625, "orderid": "ON222723-JH", "qty": 45.0, "customer": "890", "date": "1990-08-13" }, { "item": 618, "orderid": "ON222723-qa", "qty": 23.0, "customer": "870", "date": "1998-05-11" } ] – james11 Sep 15 '22 at 15:28
  • and i want this output: – james11 Sep 15 '22 at 15:30
  • and wants this output: [ { "customer": "890", "data": [ { "item": 625, "orderid": "ON222723-JH", "qty": 45.0, "customer": "890", "date": "1990-08-13" } ] }, { "customer": "870", "data": [ { "item": 618, "orderid": "ON222723-qa", "qty": 23.0, "customer": "870", "date": "1998-05-11" } ] } ] – james11 Sep 15 '22 at 15:31
  • That make sense, I was wondering that your expected output would not work with multiple customers. Can you update the output in your question. Or you can delete this and create a new one since the output is not possible with multiple and different customer IDs – Harshank Bansal Sep 15 '22 at 15:48
  • @Bansal I have updated the question with new input as well desired output. thank you – james11 Sep 15 '22 at 16:43
  • Updated the answer – Harshank Bansal Sep 15 '22 at 16:49
  • Check the formatting. Use tripple back tick '`' for posting json – Harshank Bansal Sep 15 '22 at 16:50
  • i have updated new input and output. Im looking to group by 2 fields – james11 Sep 23 '22 at 13:27
  • Please raise a separate question and revert the changes you did here. You can not just keep updating input and outputs of the same question forever, especially after earlier one is resolved. Also take care of formatting as mentioned earlier. – Harshank Bansal Sep 23 '22 at 13:30
  • I do apologize for misunderstanding of the procedures. I have updated it to the original post. Im going to post new request in a separate question @ Bansal – james11 Sep 23 '22 at 14:01