Handling apostrophes in Microsoft Flow

Almost all data in Microsoft Flow is handled using json. Just like with Xml in the past, it is important to understand the limitations of json. An example of json usage in Flow is shown below.

json action

In the above example shows:

  • An array
  • An item in an array
  • An attribute value
  • Dynamics content used within the data of json.

All of this is reasonably easy to understand.

Now it is time to look at the dynamic content.

Dynamic content in json

Consider the following flow.

json actions in Flow

The first action creates some text. The second action creates json text and the 3rd action converts this into a json object.

json works

This all works and the json object can be created as shown above.

Use double quotes

Ok, I’m aware that I can use double quotes instead as shown below. But this format isn’t always accepted.

json with double quotes

Back to apostrophes

Now imagine that the data has a single quote. I’m sure it is easy to imagine that someone leaves a comment like: I don’t like this.

single quotes

This will result in errors like the one below.

InvalidTemplate. Unable to process template language expressions in action ‘Compose’ inputs at line ‘1’ and column ‘2655’: ‘The template language function ‘json’ parameter is not valid. The provided value ‘[{ ‘value’:’don’t like this’ }]’ cannot be parsed: ‘After parsing a value an unexpected character was encountered: t. Path ‘[0].value’, line 1, position 17.’. Please see https://aka.ms/logicexpressions#json for usage details.’.

Could not Be Parsed

 

Escape the single quotes/apostrophes

With the following replace expression we can now fix this.

replace(outputs(‘Do_not_like_this’),””,’\”’)

Replace quotes

 

But what if your initial text includes a \

Now we’ve got a problem.

 

Escape failing

When you have a \ included in your text’s included in the json then you will get the following message.

InvalidTemplate. Unable to process template language expressions in action ‘Compose’ inputs at line ‘1’ and column ‘2655’: ‘The template language function ‘json’ parameter is not valid. The provided value ‘[{ ‘value’:’don\’t like this \ or maybe it is ok’ }]’ cannot be parsed: ‘Bad JSON escape sequence: \ . Path ‘[0].value’, line 1, position 32.’. Please see https://aka.ms/logicexpressions#json for usage details.’.

 

Time of the great escape!

Earlier we already used the following expression:

replace(outputs(‘Do_not_like_this’),””,’\”’)

Now we need to escape the \

My first idea was to use the following expression:

replace(replace(outputs(‘Do_not_like_this’),’\’,’\\’),””,’\”’)
But this didn’t work very well. I even tried the Parse JSON action instead of the json function. but in both cases the output is now giving me \\
double backslash
Even though the output is in my opinion not completely right here.  I expected to see:  “don\’t like this \\ or maybe it is ok”. When I use the json generated it is actually correct.
Advertisements

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.