Search code examples
muledataweavemulesoftmule4

How to use groupby and also add a new key value pairs for that particular groupby using dataweave 2.0


I have a JSON array of objects payload where I need to groupby based on multiple keys and also add two new key value pairs for each object in the groupby. First key value pair is to sum based on key value pair in that group and the second one is to get the max value in from that group. I am able to achieve till getting the groupby but not able to add new key value pairs to each object in that group. Here is the input and expected output

Input:

[
  {
    "Key1": "Digital Media",
    "Key2": "AAA",
    "Key3": "2707878341.0",
    "Key4": "116",
    "Key5": "500",
    "Key6": "66",
    "Key7": "Delivery"
  },
  {
    "Key1": "Digital Media",
    "Key2": "AAA",
    "Key3": "2707878341.0",
    "Key4": "116",
    "Key5": "50000",
    "Key6": "11",
    "Key7": "Delivery"
  },
  {
    "Key1": "Media",
    "Key2": "ABC",
    "Key3": "2446140407.0",
    "Key4": "116",
    "Key5": "1400",
    "Key6": "38",
    "Key7": "Delivery"
  }
]

Expected Output:

[
  {
    "Key1": "Digital Media",
    "Key2": "AAA",
    "Key3": "2707878341.0",
    "Key4": "116",
    "Key5": "500",
    "Key6": "66",
    "Key7": "Delivery",
    "Sum_Key6": 77,
    "Max_Key5": "50000"
  },
  {
    "Key1": "Digital Media",
    "Key2": "AAA",
    "Key3": "2707878341.0",
    "Key4": "116",
    "Key5": "50000",
    "Key6": "11",
    "Key7": "Delivery",
    "Sum_Key6": 77,
    "Max_Key5": "50000"
  },
  {
    "Key1": "Media",
    "Key2": "ABC",
    "Key3": "2446140407.0",
    "Key4": "116",
    "Key5": "1400",
    "Key6": "38",
    "Key7": "Delivery",
    "Sum_Key6": 38,
    "Max_Key5": "1400"
  }
]

Below is the script I tried but I am getting only 2 objects but I need all the 3 objects.

%dw 2.0
output application/json
---
((payload groupBy ($.Key2  ++ '_' ++ $.Key7 ++ '_' ++$.Key3)) pluck $) map {
    "Key1" : $[0].Key1,
    "Key2" : ($[0].Key2),
    "Key3" : ($[0].Key3),
    "Key4" : ($[0].Key4),
    "Key5" : $[0].Key5,
    "Key6" : $[0].Key6,
    "Key7" : $[0].Key7,
    "Sum_Key6": sum($.Key6),
    "Max_Key5": max($.Key5)
}

Can some one help me. TIA


Solution

  • This script produce what you are looking for

    %dw 2.0
    output application/json
    
    var grouped = (payload groupBy ($.MasterCustCode  ++ '_' ++ $.UsageType ++ '_' ++$.AcctIDDest))
    ---
    flatten(
    ( grouped pluck $) map (groupArray, i) -> 
        do {
            var groupSum = sum(groupArray.usagefile_UnitValue)
            var groupMax = max(groupArray.To_Unit)
            ---
            groupArray map (item, i2) -> item ++ {
                "Sum_UnitValue": groupSum,
                "Max_To_Unit": groupMax 
            }
        }
    )
    

    The groupBy strategy was good.

    I added a second map so you will go element by element insided the groups you generated.

    As the elements you are producing have the same data as the original ones plus some fields, I didn't do a full mapping. I just added the extra fields.

    At the end, as I have an array of arrays, I had to use flatten to get the final single level array.