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:

{
  "nodes": [
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Message",
              "value": "=Hello!\n{{$json[\"0\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        950,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const messages = [];\n\nmessages.push(items.map(item => {\n  return `ID: ${item.json.id}, Name: ${item.json.name}`\n}))\n\nreturn [{json:messages}]"
      },
      "name": "Create message array",
      "type": "n8n-nodes-base.function",
      "position": [
        750,
        300
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      id: 1,\n      name: \"Jim\"\n    }\n  },\n  {\n    json: {\n      id: 2,\n      name: \"Stefan\"\n    }\n  },\n  {\n    json: {\n      id: 3,\n      name: \"Hans\"\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "position": [
        550,
        300
      ],
      "typeVersion": 1
    }
  ],
  "connections": {
    "Create message array": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock Data": {
      "main": [
        [
          {
            "node": "Create message array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

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:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      id: 1,\n      name: \"Jim\"\n    }\n  },\n  {\n    json: {\n      id: 2,\n      name: \"Stefan\"\n    }\n  },\n  {\n    json: {\n      id: 3,\n      name: \"Hans\"\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "position": [
        975,
        365
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const messages = [];\n\nmessages.push(items.map(item => {\n  return `\\nID: ${item.json.id},\\nName: ${item.json.name}`\n}))\n\nreturn [{json:messages}]"
      },
      "name": "Create message array",
      "type": "n8n-nodes-base.function",
      "position": [
        1175,
        365
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Message",
              "value": "=Hello!{{$json[\"0\"].join(', ')}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1350,
        370
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Create message array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create message array": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
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: