# Aggregating List elements with the Same values in Alumio

**URL:** <https://forum.alumio.com/t/aggregating-list-elements-with-the-same-values-in-alumio/1195>\
**Category:** General\
**Created:** [June 28, 2024, 3:11pm UTC](https://forum.alumio.com/t/aggregating-list-elements-with-the-same-values-in-alumio/1195 "2024-06-28T15:11:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawid.kazibut](https://forum.alumio.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@dawid.kazibut](https://forum.alumio.com/u/dawid.kazibut)\
**Post date:** [June 28, 2024, 3:11pm UTC](https://forum.alumio.com/t/aggregating-list-elements-with-the-same-values-in-alumio/1195/1 "2024-06-28T15:11:44Z")

</div>

Hello,

I have an order JSON with two order lines that contain the same SKU. I need to aggregate the lines with the same SKU. Could you please advise how to do it?

Input:  
{  
“Lines”: [  
{  
“Id”: 79,  
“Quantity”: 1,  
“MerchantProductNo”: “5895”  
},  
{  
“Id”: 80,  
“Quantity”: 1,  
“MerchantProductNo”: “5895”  
},  
{  
“Id”: 80,  
“Quantity”: 1,  
“MerchantProductNo”: “5666”  
}  
]  
}

Expected Output:  
{  
“Lines”: [  
{  
“Id”: 79,  
**“Quantity”: 2,**  
“MerchantProductNo”: “5895”  
},  
{  
“Id”: 81,  
“Quantity”: 1,  
“MerchantProductNo”: “5666”  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![Gugi](https://forum.alumio.com/user_avatar/forum.alumio.com/gugi/32/236_2.png) [@Gugi](https://forum.alumio.com/u/Gugi)\
**Post date:** [July 1, 2024, 12:27pm UTC](https://forum.alumio.com/t/aggregating-list-elements-with-the-same-values-in-alumio/1195/3 "2024-07-01T12:27:47Z")

</div>

Hi @dawid.kazibut

Welcome to the Alumio forum.

You can group objects with the same criteria, such as the same MerchantProductNo, by using the [Group Records](https://forum.alumio.com/t/how-to-group-records/279) transformer.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/0/064720d00e8316b90949702c4a13c5b5d2fef24d.png)

Result:

```auto
{
  "Lines": {
    "5666": [
      {
        "Id": 80,
        "Quantity": 1,
        "MerchantProductNo": "5666"
      }
    ],
    "5895": [
      {
        "Id": 79,
        "Quantity": 1,
        "MerchantProductNo": "5895"
      },
      {
        "Id": 80,
        "Quantity": 1,
        "MerchantProductNo": "5895"
      }
    ]
  }
}

```

Then, you can loop through the `Lines` array to get the aggregate of the `Quantity` value of the grouped records. We suggest to use JMESPath `sum` function to do so.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/0/0ad26e7a5e446b2743c994ff54443d0c0e30d3f8.png)

Values of the Value setter.

```auto
{
  "Id": "&{[].Id | [0]}",
  "Quantity": "&{[].Quantity | sum(@)}",
  "MerchantProductNo": "&{[].MerchantProductNo | [0]}"
}

```

Result:

```auto
{
  "Lines": {
    "5666": {
      "0": {
        "Id": 80,
        "Quantity": 1,
        "MerchantProductNo": "5666"
      },
      "result": {
        "Id": 80,
        "Quantity": 1,
        "MerchantProductNo": "5666"
      }
    },
    "5895": {
      "0": {
        "Id": 79,
        "Quantity": 1,
        "MerchantProductNo": "5895"
      },
      "1": {
        "Id": 80,
        "Quantity": 1,
        "MerchantProductNo": "5895"
      },
      "result": {
        "Id": 79,
        "Quantity": 2,
        "MerchantProductNo": "5895"
      }
    }
  }
}

```

Then, we should remove the non-aggregated objects, as you will only need the `result` property.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/e/e26d213685dbd89a0b9842f5790b9426bbaf96a4.png)

Result:

```auto
{
  "Lines": {
    "5666": {
      "Id": 80,
      "Quantity": 1,
      "MerchantProductNo": "5666"
    },
    "5895": {
      "Id": 79,
      "Quantity": 2,
      "MerchantProductNo": "5895"
    }
  }
}

```

The `Lines` array is now an object (associated array) whose keys are strings. We can convert it to an indexed array using a mapper called “List: Get values”.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/4/4047889ecb7a7ba1afc0eef5ce6fbd20cbddfa69.png)

Result:

```auto
{
  "Lines": [
    {
      "Id": 79,
      "Quantity": 2,
      "MerchantProductNo": "5895"
    },
    {
      "Id": 80,
      "Quantity": 1,
      "MerchantProductNo": "5666"
    }
  ]
}

```
