cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Mattw112IG
Impactful Individual
Impactful Individual

Escaping JSON Invalid Characters

Has anyone already built a flow to escape invalid characters in JSON?

 

I'm going to build it but thought I would check first.

 

Thanks,

Terry

6 REPLIES 6
SCTdan
Continued Contributor
Continued Contributor

Can you explain a bit more what you're trying to do?  Post example data if you have any

Mattw112IG
Impactful Individual
Impactful Individual

In Flow I was creating SharePoint site pages using HTTP Rest.

The Description field I was passing has characters that JSON considers invalid characters.

In order to use those characters they have to be escaped.  typically with a backslash.

 

Quotation mark (") \"
Backslash (\) \\
Slash (/) \/
Backspace \b
Form feed \f
New line \n
Carriage return \r
Horizontal tab \t

as well as some control characters like '  \u2019

 

Anyway, it was simple replace operation, although I used multiple compose.

 

But was just wondering if someone already had something.  Since I didn't see anything I just moved ahead.

 

Thanks,

Terry

SCTdan
Continued Contributor
Continued Contributor

Gotcha.  Yea I've always just used replace() as well for that.

Mattw112IG
Impactful Individual
Impactful Individual

There's just a bunch of them, so was hoping to copy and paste 😉

 

One lesson learned for anyone reading this though:

 

Do the backslash replace first, before escaping the others, otherwise the backslash replaces the escape backslashes. lol.  so you end up getting \\\ or \\/ instead of what it should be.

 

Oh second lesson learned. I already knew this, but didn't think about it until was testing.. there are multiple types of double quotes.  You have to escape/convert all of them (“,”,") to the standard \"

 

Terry

MortenT
Advocate I
Advocate I

There should be a function that does all the replacement.. Right??

Has anyone found that one?

I agree that would be nice but in lieu of that hope fully this will help.

 

replace(replace(replace(replace(replace(<string_data>,'\','\\'),'/','\/'),'"','\"'),'“','\"'),'”','\"')
 
It does make for a gnarly function but I think that takes what @Mattw112IG suggested and implements (with the replaces in the right order so you don't end escaping \" to \\" which then creates an invalid json string)

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

New Ideas Forum MPA.jpg

A new place to submit your Ideas for Power Automate

Announcing a new way to share your feedback with the Power Automate Team.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

MPA Licensing.jpg

Ask your licensing questions at the Power Automate AMA!

Join Priya Kodukula and the licensing team, super users and MVPs to find answers to your questions on Power Automate licensing.

Users online (1,863)