How to group an array by one item?

Hello n8n community! This is my first question, hope you can give me a hand :grinning:

I’m using a GET request to retrieve a JSON with all the orders of the business. The output of the request gives me all the orders in an array. An order could appear 1, 2 3 or X times in the array, individually, since each appearance reports 1 item bought by the customer. If the order has 7 items, it will appear 7 times in the array. Example: order 297 has 2 items, so, the array looks like this:

[
  {
    "id": 2019,
    "created_at": 1684287966651,
    "c_linea": "1",
    "d_almacen": "BLUSH BAR ZONA T",
    "d_clasificacion": "MUESTRA",
    "c_producto": "13384",
    "d_producto": "BB GWP MAYO 2023 COLOMBIA",
    "sku": "13386",
    "d_ciudad": "BOGOTA",
    "f_creacion": "2023-01-05",
    "nombre": "YENNIFER ARBOLEDA",
    "cc_cliente": "XX",
    "nombre_completo": "MARIA CAMILA MADRID",
    "order_number": "303",
    "pr_compra": "6509"
  },
  {
    "id": 2020,
    "created_at": 1684287966651,
    "c_linea": "1",
    "d_almacen": "BLUSH BAR ZONA T",
    "d_clasificacion": "MUESTRA",
    "c_producto": "13384",
    "d_producto": "BB GWP MAYO 2023 COLOMBIA",
    "sku": "13386",
    "d_ciudad": "BOGOTA",
    "f_creacion": "2023-01-05",
    "nombre": "KAREN  MUÑOZ",
    "cc_cliente": "XXX",
    "nombre_completo": "adriana almonacid",
    "order_number": "297",
    "pr_compra": "6509"
  },
  {
    "id": 2068,
    "created_at": 1684287966651,
    "c_linea": "2",
    "d_almacen": "BLUSH BAR ZONA T",
    "d_clasificacion": "VENDIBLE",
    "c_producto": "10527",
    "d_producto": "ABH STICK BLUSH - PEACHY KEEN",
    "sku": "10527",
    "d_ciudad": "BOGOTA",
    "f_creacion": "2023-01-05",
    "nombre": "YENNIFER ARBOLEDA",
    "cc_cliente": "XXX",
    "nombre_completo": "MARIA CAMILA MADRID",
    "order_number": "303",
    "pr_compra": "63494"
  },
  {
    "id": 2134,
    "created_at": 1684287966651,
    "c_linea": "2",
    "d_almacen": "BLUSH BAR ZONA T",
    "d_clasificacion": "VENDIBLE",
    "c_producto": "6364",
    "d_producto": "ABH HIGHLIGHTING DUO PENCIL - MATTE CAMI",
    "sku": "6338",
    "d_ciudad": "BOGOTA",
    "f_creacion": "2023-01-05",
    "nombre": "KAREN  MUÑOZ",
    "cc_cliente": "XXXX",
    "nombre_completo": "adriana almonacid",
    "order_number": "297",
    "pr_compra": "54243"
  }
]

I want to group all the orders by the order_number parameter, and conduct some operations (sum(pr_compra) , count(items bought)), expecting something like this:

{
"order_numer":"297"
   "Data":[
         "f_creacion": "2023-01-05",
          "total": "60752"]
}

I’m used to this same operation in Make.com, using the array aggregator but I’m having trouble finding the solution in n8n.

Thanks! I’ll be looking forward to clarifying and solve questions

Hi @Lucas_Vargas, welcome to the community :tada:

This is something you can do using the Set + Item Lists nodes in n8n. Assuming you want to have one field order_number and an array field Data containing all the items for each order number, the workflow could look like so:

This is the result:

Hope this helps :slight_smile:

2 Likes

Yes! This worked perfectly. It was easy to understand and easy to apply! Thank you so much!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.