Dynamic String building over multiple input items

Hello,
I would like to dynamically combine strings from multiple items into a new string that I can then process.

Specifically, I have the following example:
I read a database and get the following items: Each of these items contains the element Text, Type and ID.
I want that every time an item has the Type:Headline the text of this item together with the texts of the following items are combined into a new string until the next item with the Type: Headline.
For example, in the attached example, the texts of the items with IDs 3-10 should be combined into a single text string. The next blocks would then be IDs 11-13 and 14-17.

I have already tried a few things but I just can’t find a suitable solution how to achieve this. Maybe someone here can help me or give me a tip on how to implement this.

Thanks a lot

[
{
"id":1,
"order":"1.00000000000000000000",
"Text":"Component elements",
"Type":"Headline"
},
{
"id":2,
"order":"2.00000000000000000000",
"Text":"Ceramic capacitors",
"Type":"Headline"
},
{
"id":3,
"order":"3.00000000000000000000",
"Text":"Rule 5",
"Type":"Headline"
},
{
"id":4,
"order":"4.00000000000000000000",
"Text":"When using ceramic chip capacitors .... Suggestions:",
"Type":""
},
{
"id":5,
"order":"5.00000000000000000000",
"Text":"Use of series ...",
"Type":""
},
{
"id":6,
"order":"6.00000000000000000000",
"Text":"Use of ceramic capacitors ....",
"Type":""
},
{
"id":7,
"order":"9.00000000000000000000",
"Text":"Use of ceramic capacitors with ....",
"Type":""
},
{
"id":8,
"order":"10.00000000000000000000",
"Text":"Use of ceramic capacitors with integrated ...",
"Type":""
},
{
"id":9,
"order":"12.00000000000000000000",
"Text":"Two series-connected ceramic capacitors arranged ...",
"Type":""
},
{
"id":10,
"order":"13.00000000000000000000",
"Text":"Use of wired ceramic capacitors",
"Type":""
},
{
"id":11,
"order":"14.00000000000000000000",
"Text":"Rule 7",
"Type":"Headline"
},
{
"id":12,
"order":"15.00000000000000000000",
"Text":"Voltage strength:",
"Type":""
},
{
"id":13,
"order":"16.00000000000000000000",
"Text":"For 12 V applications, types with a voltage strength of ....",
"Type":""
},
{
"id":14,
"order":"18.00000000000000000000",
"Text":"Rule 8",
"Type":"Headline"
},
{
"id":15,
"order":"19.00000000000000000000",
"Text":"Type:",
"Type":""
},
{
"id":16,
"order":"20.00000000000000000000",
"Text":"Capacitors with a capacity/package at ....",
"Type":""
},
{
"id":17,
"order":"21.00000000000000000000",
"Text":"The use of the 1206 package shall ....",
"Type":""
}
]

Information on n8n setup

  • n8n Version 0.209.2
  • default: SQLite
  • Running n8n via Docker

Hey @n8n_BLN,

Welcome to the community :cake:

Whenever I have to build out a message for sending an email or say a Slack message I use a Code node to build it out, As a quick example this workflow might be a good starting point for you.

Thank you very much! The code works fine, but I noticed that the IDs 14-17 are not processed. Do you have any idea why this is?

okay, I think I found out why. The last Text will not be pushed/returned cause there is no Headline element at the end which fullfills the the if loop

got it running by adding another if statement at the end

let result = [];
let message = '';
for (const item of $input.all()) {
  if (item.json.Type === 'Headline') {
    if (message !== '') {
      result.push({message});
      message = '';
    }
    message += `${item.json.Text} `;
  } else {
    message += `${item.json.Text}`;
  }
}
**if (message) {**
**    result.push({message});**
**}**
return result;

Thanks again Jon!!

Ah nice you worked it out :slight_smile:

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