Search code examples
mulemule-studiodataweavemulesoftmule4

Need to add similar customer into array after performing grouBy


I am fetching customer contacts from salesforce which are coming as array of objects as shown below

[
    {
        "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 output to be array of arrays, each sub array should have all the customer details of atmost three different customers.

Reuired 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"
        }
    ]
]

Need to send this array of arrays to parallel for each for parallel processing.


Solution

  • GroupBy for grouping according to customerID.

    pluck for converting object with key as numbers to array.

    divideBy 3 for seggregating into group of numbers eg [[[1],[2],[3]],[[4],[5],[6]],[[7]]]

    map for iterating through nested array[array of objects]

    flatten for converting nested array [[[]]] to single array [[]]

    %dw 2.0
    output application/json
    import * from dw::core::Arrays
    ---
    payload groupBy $.customerID pluck $ divideBy 3 map((flatten($)))
    

    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": "Jonhn17"
        }
      ]
    ]