Apostrophe

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.

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'),'''','\''')
handling apostrophes using replace function

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.

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

4 thoughts on “Handling apostrophes in a Flow”
    1. 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.

  1. Hi,

    Please can I get some help with this. In the string being passed through my flow there is a ‘ for examples Butler’s. I have tried replace(outputs(‘Get_source_event’)?[‘body/Title’],””,’\”’) and it just is not working and constantly throughs me the error of: Invalid JSON. A comma character ‘,’ was expected in scope ‘Object’.

    Is there anything I can do here? I don’t want to have to tell the user they can’t user apostrophes.

    Thanks

    1. Hi Jack,

      In short you will want to escape the ‘ in the text fields where there are single quotes possible. Escaping can be done by replacing the quotes with \’ or two single quotes ”

Leave a Reply

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

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue reading