Text manipulation & setting new variables based on if/else

Hi, I have some questions about doing if/then statements and setting new values based on the results, when working with text.

I query MySQL, get a bunch of data, and then post that data elsewhere.

Simple example:

Twitter: https://twitter.com/twitterdev
Discord: https://discord.com/invite/sample
Instagram: https://www.instagram.com/username
Youtube: https://youtube.com/username
Telegram: https://t.me/username

The data from MySQL comes as a URL (i.e. https://twitter.com/twitterdev).

I want something like:

IF twitter_url contains https, do "Twitter: Twitter: [twitter_url]" ELSE do nothing
IF discord_url contains https, do "Discord: [discord_url]" ELSE do nothing

Or maybe a better way to think of it:

IF twitter_url contains https, set var twitter_publish to "Twitter: [twitter_url]" ELSE do nothing
IF discord_url contains https, set var discord_publish to "Discord: [discord_url]" ELSE do nothing

So for one that only has Instagram and YouTube, the final result would be:

Instagram: https://www.instagram.com/username
Youtube: https://youtube.com/username

I do not want:

Twitter:
Discord:
Instagram: https://www.instagram.com/username
Youtube: https://youtube.com/username
Telegram:

I can’t use IF nodes (if URL exists, true) followed by a SET node (set variable “twitter_line” to “Twitter: [twitter_url]”), because there are too many to re-join back with a Merge node, as it only accepts 2 inputs, and I will have a total of 10-15.

I’ve spent a lot of time looking up javascript to do this, but I don’t understand how to apply the Javascript to the n8n expressions editor. For example, I found “includes” like:

{{$json["twitter_link"].includes(http)}}

But that just returns true or false. I need it to DO something based on the result - output nothing, or output for example, “Twitter: [“twitter_link”]”

With the code editor, I don’t understand how to pull in the actual value from a previous node (i.e. [twitter_link] in, and how to make it output a new variable based on the result, like:

var twitter_publish = (twitter_link === "") ? "":"Twitter: [twitter_link]";

I’m coming from another platform and am really struggling with text manipulation in general with n8n. Without knowing javascript, it seems like I’m really limited with what I can do with text. There are a ton of things n8n does really well, I’m not complaining, I’ve just been spinning my wheels all day on this, and other similar text manipulation problems, and I’m no better off then I was 8 hours ago.

In Integromat/Make, what I’m trying to do is like:

ifempty(Twitter: twitter_url);"")

Thank you.

Hi @mmac, I am not 100% sure I understand the structure of your incoming data. Based on your description I assume it’s something like this?

image

Now if you want a single item with a single field with a value of

Instagram: https://www.instagram.com/username
Youtube: https://youtube.com/username

there is unfortunately no built-in node to achieve this I could think of. But you could write a bit of custom code for this like so:

I’ve added comments on what each line of code does, but it might still be hard to get the hang without JS. There is a community node by @lublak which helps with several text transformations, perhaps you might want to take a look at it?

And if you have any questions about the text manipulation node, feel free to ask. :slight_smile:

The data comes like this from MySQL:

[
{
"twitter_link":
"https://twitter.com/twittersupport",
"twitter_handle":
"twittersupport",
"twitter_followers":
"4673",
"discord_link":
"",
"discord_invite_id":
"",
"discord_guild_id":
"",
"discord_members":
"",
"discord_online":
"",
"telegram_link":
"https://t.me/somegroup",
"telegram_at":
"somegroup",
"telegram_members":
"18",
"telegram_online":
""
},
{
"twitter_link":
"https://twitter.com/anotheraccount",
"twitter_handle":
"anotheraccount",
"twitter_followers":
"34564",
"discord_link":
"",
"discord_invite_id":
"",
"discord_guild_id":
"",
"discord_members":
"",
"discord_online":
"",
"telegram_link":
"https://t.me/anothergroup",
"telegram_at":
"anothergroup",
"telegram_members":
"89",
"telegram_online":
"5"
} ]

The code you gave didn’t work, I assume that’s because I have a different data structure than you thought. I don’t know JS so I’m not able to do much to edit it to work.

@lublak I installed the Text Manipulation node, thank you. I see there is a concat, so I can do "Twitter: " before the [twitter_url] field and it will produce, “Twitter: https://twitter.com/somename” .

But I need it to produce nothing/null if there is no URL in the [twitter_url] field, and I didn’t see a way to do any conditional logic.

But this would’ve saved me a lot of time with some regex / replace / change case / trim I have in some other workflows, so even if it can’t do what I’m lookin for here, it’ll be very helpful going forward. Thank you!

@mmac you can do it with the Text Manipulation Node:

Ah fantastic! I’ve got it working and it’s fabulous!

1 Like

@lublak I’m trying to use “Trim” and it’s giving the error, ERROR: Cannot read properties of null (reading ‘replace’)" if a field I am trying to trim doesn’t exist.

Example below:

The field “Telegram” is null, and since it can’t trim a null field, it gives an error.

I tried with “Continue on Fail” and “Always Output Data” on, but that doesn’t solve the problem.

And unlike the “Concat” example you gave above, there is no “Skip Non-String” option, so I can’t just bypass these empty fields.

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