Help needed with loop and data concatenation

Describe the problem/error/question

I’m working on a workflow to generate delivery slip for woocommerce. To do that I need to provide the PDF generator API with the delivery slip content: order id, customer address, order date etc… but also list of ordered product with descriotion.

I’m now struggeling with product description as I need it to be an agragation of the product name and all the attributes (options).

For example:

Product name: A fancy t-shirt
Attributes:

  • color: blue
  • size : medium

At the end of the workflow I’d like to have something like:

Product description: “A fancy t-shirt, color: blue, size: medium”

What is the error message (if any)?

My issue here is that when I loop the product attribute to update the description by adding the each attribute I end with as many product description as the number of attribute.

For example:

[
{
"product_description": 
"my product description\nSize : Small"
},
{
"product_description": 
"my product description\nColor : Orange"
}
]

How can I have at then end only one result with all the attributes ?

Here is my test workflow to illustrate the issue

Share the output returned by the last node

Here is the last step output:


[
{
"order": 
[
],
"product_description": 
[
"my product description\nSize : Small",
"my product description\nColor : Orange"
],
"order_id": 
[
],
"customer_name": 
[
]
}
]

I was expecting to have:


[
{
"order": 
[
],
"product_description": "my product description\nSize : Small\nColor : Orange",
"order_id": ,
"customer_name": 
}
]

Information on your n8n setup

  • I’m running n8n on a Hostinger VPS, version update-to-date

Thanks in advance for your help

Hi @garulfo2

I’m not exactly sure I understood you, but based on the workflow,
here’s my attempt:

the output:

Hey ! Thanks for trying, but unfrotunaltely this is not what I expect. In you result you ave twice the same product but with different attribute. I need one product only with a concatenated attributes like:

[
   {
      "product_description":"my product description\nSize : Small,\nColor : Orange",
      "order_id":1234,
      "customer_name":"John Doe"
   }
]

As you can see in this example, the product description agregate the product name and all the attributes in one string.

I think i got it,
hope this is what you looking for:

Yes, that’s it. Interesting solution, thanks !
In between I have also been able to do that using code node with a Javascript function which is doing a similar action.

It works but I feel that both options are a bit complicated just to concatenate strings. Simple variable management would be so much clearer and easy to setup.

Thanks a lot for your help !

For those who are interested, here is my javascript function:

// const productName = $('NomDuNoeudProduit').first().json.productName;
const productName = $('Set product description').first().json.product_description +`\n`;
const attributes = $('Set attributes').first().json.attributes;
const variation_id = $('Get variation').first().json.id;
const quantity = $('Split line items').first().json.quantity;
const provider = $('Get a product').first().json.brands[0].name

let result = productName;
attributes.forEach(attr => {
  result += `${attr.name}: ${attr.option}\n`;
});

return [
  {
    oder_items: {
      description: result,
      quantity: quantity,
      sku: variation_id      
    }
  }
];

Hi @garulfo2

The issue is that your loop is creating a separate output item for each attribute instead of updating a single string. Each time the loop runs, it references the original “my product description” and appends only the current attribute, which is why you get two separate results.

The best solution is to avoid the loop entirely and format the string in a single step.

The Solution

First, remove the Split Out , Loop Over Items , and Aggregate nodes from your workflow.

Next, after your “Set attributes” node, add a new Set node. Inside this new node, create a field named product_description.

Set its value using this expression:

  const baseDesc = $('Set product description').item.json.product_description;
  const attributes = $('Set attributes').item.json.attributes;
  const attributeText = attributes.map(attr => `${attr.name} : ${attr.option}`).join('\n');
  return `${baseDesc}\n${attributeText}`;
}}

This expression takes the initial description and the full attributes array. The .map() function transforms the array into a cleaner format (like [“Size : Small”, “Color : Orange”]), and the .join(‘\n’) function combines them into a single string separated by new lines. Finally, it returns the complete, properly formatted description in one go.