Multiple Objects in a single text output (Email, Airtable, Github etc.) – Looping within an Expression Field

Hey there,

I am currently challenged with a task that I wanted to quickly share with the community to see if I am missing something that could potentially help me out.

Let‘s say we do have an input from a node that could be displayed like below:

[
  {
    title: "Event One"
    start_time: "5pm"
    location: "First Ave"
  },
  {
    title: "Event Two"
    start_time: "6pm"
    location: "Second Ave"
  },
  {
    title: "Event Three"
    start_time: "7pm"
    location: "Third Ave"
  }
]

I now like to use that data in a node that works with (more or less) plain text, like the Send Email, Airtable, or the Github node.

Naturally I would combine the elements in a function node to receive something like:

[
  {
    data: [
      {...}
    ]
  }
]

But that does not really help in that situation as I am trying to achieve the following output:

You successfully signed up for the following events:

"Event One" starting at 5pm on First Ave.

"Event Two" starting at 6pm on Second Ave.

"Event Three" starting at 7pm on Third Ave.

So I am basically looking for a solution that allows me to loop over the input object within an Expression Text Field.

Is there anything available like that? Something in the works? A workaround that I could utilize?

Thanks a lot for your time and the help. I really do appreciate that :slight_smile:

Hey @bastian!

Maybe this might help:

Thanks for the suggestion @harshil1712,

in order to generate an actual ready-to-be-shipped email, I would need the possibility to include linebreaks in the final result.

Currently the output of your example would look like this:

[
  {
    "Message": "Hello! ID: 1, Name: Jim,ID: 2, Name: Stefan,ID: 3, Name: Hans"
  }
]

Which would result in an formatted email without any linebreaks.

Hello! ID: 1, Name: Jim,ID: 2, Name: Stefan,ID: 3, Name: Hans

while it ideally should be displayed like below:

Hello! 

ID: 1,
Name: Jim,

ID: 2, 
Name: Stefan,

ID: 3, 
Name: Hans

I know that this might be impossible due to some limitations of JSON (if I am not mistaken) but I think there is a valid usecase to implement a solution that allows for some kind of formatting – especially in regards to some API endpoints that already have been implemented but could profit from such a addon.

Happy to hear your thoughts about that – maybe I am also missing the obvious :smiley:

You can use escape characters to format the output. So in the second Function node, add \n where you want to add a line. In the node where you want to reference the value, you can change the expression to {{$json["0"].join(', ')}}. I tested it with the Discord node and it works as expected. I get values in new lines. In the Set node they might appear to be in the same line, but I would suggest you to send a test email and see if you get the desired output.

Here’s the updated workflow:

1 Like

Wonderful @harshil1712 :smiley:

That was exactly what I was looking for. Works totally fine on both email and the Github API.

For others who might see that result:
I actually had to re-add the escaped \n characters as the ones added have been not working and simply added a \n to the output – but once that had been down it worked flawlessly.

Thanks so much for your support and time.

1 Like

Thanks for sharing! I am happy that it is working now! Have fun :tada: