Try for free Book a demo

Logic App Best Practices, Tips, and Tricks: #37 How to handle special characters inside Logic Apps actions?

Microsoft Azure

4 Mins Read

Handle Special Characters Inside Logic Apps Actions

Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I discussed some of the essential best practices you should use while working with Azure Logic Apps.

Today, I will speak about another useful Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): How to handle special chars inside Logic Apps actions.

How to handle special chars inside Logic Apps actions

How to handle special characters within Logic Apps actions?

While working with Logic Apps, I found many scenarios where I needed to manage special characters or symbols like:

  • Splitting a string by a new line (\r\n that is equal to CR = Carriage Return + LF = Line Feed → Used as a new line character in Windows)
  • Or to sanitize a string by removing certain special characters like \t (tab)

However, what became evident to me was that the methods for performing these operations, which can indeed be accomplished using out-of-the-box actions within Logic Apps, were somewhat unconventional or, in simpler terms, not particularly user-friendly.

As an illustration, consider the newline exercise in which we aim to divide a string based on line breaks. Here, we will be receiving the following payload or string:

Sandro;Pereira;1978;Crestuma;4415
José;Silva;1972;Crestuma;4415
Rui;Barbosa;1975;Lever;4415
Diogo;Formosinho;1999;Crestuma;4415
Luís;Rigueira;1989;Viseu;3500

And the goal, of course, is to create an array of lines. For that, we can:

  • Use a Data Operations > Compose action, and on the Inputs property, put the following expression
split(triggerBody(),'\r\n')

Dealing with Special Characters within Logic Apps Actions

At first glance, this might be our initial impression and approach, the reality unfolds quite differently. If you test your Logic App, you will realize that the action did not do as expected and in reality, the Input is exactly the same as the Output:

How to deal with Special Characters within Logic Apps Actions

Then you may think, I probably need to escape the backslash (\) on my split expression, and then  you change it for something like this:

split(triggerBody(),'\\r\\n')

If you test it, you will notice again that the action did not do as expected, and in reality, the Input is exactly the same as the Output.

Now, I want to emphasize something crucial! Within the Logic App design, there’s no way to get this functionality working! And the reason why is that anything string you put on the parameters of the action is already escaped automatically behind the scenes. That means when you type ‘\r\n’ in the split expression, behind the scenes the Code View generates:

"inputs": "@split(triggerBody(),'\\r\\n')"

Logic Apps Actions and Special Characters

Now, the critical question arises: How can we tackle this issue? How do we accomplish string splitting by a newline?

The answer is very simple. To accomplish this, we need to:

  • Indeed, set the Inputs parameter of the Data Operations > Compose action to be
split(triggerBody(),'\r\n')
  • But then, we need to switch to Code View, by clicking </> Code view

Dealing with Special Characters in Logic Apps

  • And in Code view, fix the second parameter of the split expression to be ‘\r\n’ instead of ‘\\r\\n’ 

How to deal with Special Characters within Logic Apps Actions

  • Now, when you return to the Designer and open the Compose action, you’ll notice that our split expression appears altered in the Expression window. It may seem somewhat incomplete, but that’s not the case. In fact, it includes a line break, and if you scroll down, you’ll find it extends over two lines.

How to deal with Special Characters within Logic Apps Actions

Dealing with Special Characters within Logic Apps Actions

Now, if you save it and test your Logic App again, we will see that the Data Operations > Compose action output will be an array of strings split by the new line.

Dealing with Special Characters in Logic Apps

Now, in the other scenario where we need or want to sanitize a string by removing certain special characters like a tab (\t), it will be exactly the same story. We need to go to Code View and change, in this case, the replace expression from ‘\\t’ to ‘\t’ in the second parameter of the expression:

Logic Apps Actions and Special Characters

Now, when you return to the Designer and open the action, you will see a tab representation inside the expression:

Dealing with Special Characters in Logic Apps

As I mentioned at the beginning, this method is somewhat unconventional or, in simpler terms, not particularly user-friendly, but it will do the trick!

I hope you enjoy this developer tip and stay tuned for the following Logic App best practices, tips, and tricks.

Related reading

This article was published on Oct 10, 2023.

Azure Logic Apps - Request Demo CTA

Related Articles