Grouping or Aggregating items in workflow based on a field value

Hello Everyone,

I am still getting used to n8n and I am stuck on how to group items in a workflow.

Issue:
I am doing an (one) API call (via the HTTP node) to get list of all the products sold by an online shop/hub. Now, this shop sells over 1000+ products from 10+ producers/brands.
So, I get 1000+ items in the workflow with each item containing fields such as product name, price, quantity, producer_id etc.
What I want is to group all the items based on their producer_id, so I can generate multiple CSV files with one for each producer containing list of products from that producer.

What’s happening currently is, I am getting one giant CSV file with all the products.
I am using the spreadsheets node for generating a CSV at the end.

Note: This is a simplified explanation of the workflow. The HTTP call gives only 1 item (containing all the products) as an output, then I use split items node to get all the products separately as items.
Also, there is no fixed allocation of products across producers, i.e. producer 1 to 10 can have any number of products linked to them.

I tried using Item List node with Aggregate items option, but there is no way to group items based on a field value (producer_id in my case). Same issue with split in batches node.

Information on your n8n setup

  • n8n version: 0.195.5
  • **Running n8n via [Docker, npm, n8n.cloud, desktop app]:**n8n.cloud

Do you have some sample input data? It’s not clear why item lists can’t aggregate on the producer_id field.

Hi @pemontto,
This is a sample/example output from (API call + SET module) to filter out unnecessary fields (for this mock data, I have limited the output to 10 items, with each item representing 1 product):

[
  {
    "id": 1612,
    "producer_id": 131,
    "on_hand": 1,
    "price": "1.12",
    "product_name": "Yarrow - Terracotta"
  },
  {
    "id": 1611,
    "producer_id": 131,
    "on_hand": 2,
    "price": "2.75",
    "product_name": "Rose - Distant Drums"
  },
  {
    "id": 1610,
    "producer_id": 145,
    "on_hand": 0,
    "price": "2.75",
    "product_name": "Rose - Distant Drums"
  },
  {
    "id": 1609,
    "producer_id": 145,
    "on_hand": 4,
    "price": "10.0",
    "product_name": "Falda Light Apricot"
  },
  {
    "id": 1608,
    "producer_id": 145,
    "on_hand": 5,
    "price": "5.23",
    "product_name": "Northern Sea Oats"
  },
  {
    "id": 1606,
    "producer_id": 223,
    "on_hand": 10,
    "price": "6.38",
    "product_name": "Pomegranate"
  },
  {
    "id": 593,
    "producer_id": 145,
    "on_hand": 3,
    "price": "6.38",
    "product_name": "Strawflower - Golden Short"
  },
  {
    "id": 592,
    "producer_id": 223,
    "on_hand": 7,
    "price": "8.27",
    "product_name": "Celway Pink Mix"
  },
  {
    "id": 591,
    "producer_id": 145,
    "on_hand": 7,
    "price": "9.93",
    "product_name": "Ornamental - White"
  },
  {
    "id": 590,
    "producer_id": 145,
    "on_hand": 2,
    "price": "9.43",
    "product_name": "Ornamental - Pink"
  }
]

This sample data contains 10 products from 3 producers (identified by the producer_id field - 131,145, and 223). Now, I want to generate 3 CSV files containing all the fields (id, price, on_hand, product_name, producer_id), one for each producer.

Therefore,
Producer no. 131’s CSV should have 2 products, Producer 145’s CSV should have 6 products, and Producer 223’s CSV should have 2 products.

When I use Spreadsheet module directly, I get everything in 1 CSV (Which I don’t want)

Using Item List: Aggregate Module, I get different arrays/list for each field I choose to aggregate, rather than all the field grouped by producer_id.

Ahh an interesting problem because of the dynamic number of producer IDs.

This workflow should help, it grabs the first producer ID, then sets an export flag used in the IF condition. It loops around until all items are exported:

1 Like

Thanks @pemontto! It works perfectly :raised_hands: