Handling apostrophes in a Flow

In this post i will look at handling apostrophes in Power Automate.

Json in Power Automate

Almost all data in Power Automate 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.

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.

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

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.

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.

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.’.

Escape the single quotes/apostrophes

With the following replace expression we can now fix this.

replace(outputs('Do_not_like_this'),'''','\''')

But what if your initial text includes a \

Now we’ve got a problem.

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 \

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.

Share
Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

View Comments

    • Two single quotes could possibly do it to but I find it becoming very complicated to escape the escaping single quote. It becomes very confusing when you have to put 4 or 8 quotes in place.

Recent Posts

Calculate the Sum for a SharePoint column in Power Automate

Last week Shane Young asked me about calculating the Sum for a SharePoint column in…

2 days ago

Object must implement IConvertible in Power Apps

In Power Apps when you do a Patch to create a new item or to…

6 days ago

Get started with adaptive cards in Power Automate

Getting started with adaptive cards can be difficult. In this post i have written some…

2 weeks ago

Create PDF documents from data in Power Automate

In this post I will look at how to create PDF documents from data. Use…

2 weeks ago

Unnest nested arrays in Power Automate

We all know this problem, you have a nested array in Power Automate but how…

3 weeks ago

Advanced settings not loading in Power Platform

Yesterday one of my clients showed me an issues where the Advanced settings didn't load.…

4 weeks ago
%%footer%%