Add variable lines from one step to another

Hello!

I would like to add values that I receive from another step, but the number of values is not always the same; sometimes there are 5, and other times 10. The JSON body is as follows:

{
  "Id": "Id",
  "status": "status",
  "title": "Title",
  "body": {
    "value": "
    VALUES FROM ANOTHER JSON
    "
  }
}

Could you please advise me on the best approach to handle this?

Thank you very much for your time and assistance.

Best regards,

Álvaro

1 Like

Hi @aboza_1

just drop an Aggregate node right before the step where you build that JSON. set it to ‘Aggregate All Item Data’. this catches all incoming items and squashes them into a single array on one item.

then in your next node, map that new array into your body.value field. if your API expects plain text instead of a raw array, just use an expression like ={{ JSON.stringify($json.data) }}.

handles the dynamic sizing perfectly from what i’ve seen. doesn’t matter if 2 items come through or 20.

Mastering the n8n Aggregate Node
this video breaks down exactly how to configure the aggregate node to combine multiple separate items into one single output if you need a visual walkthrough.

hope this will help you!

2 Likes

The Aggregate node is the right call here, but if you want to skip adding an extra node you can also just pull all the values directly in your expression using {{ $('YourPreviousNode').all().map(item => item.json.yourField).join('\n') }} — that grabs every item regardless of count and joins them into a string. If your API needs actual JSON array format instead of a string then swap the .join() for JSON.stringify() on the whole thing. Either way handles the variable item count without any issues.

Hi @aboza_1 Welcome to the community!
As long as the value is going to be an integer there would no problem in using the code node to perform that action using javascript, and making guard clauses that would prevent the program from running into an error, just use a code node and things would work out easily.

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