Escaping Quotes in HTML Links Inside a JSON Body in Power Automate / Flow

I recently had a request where the out-of-the-box email connectors weren't an option. The client preferred an internal API that, among other things, expected the body of the email to be passed as HTML inside a JSON package. Easy-peasy...right up until I needed a hyperlink.
The Problem
Here's roughly what the body of my HTTP action looked like:
{
"To": "someone@contoso.com",
"Subject": "Your report is ready",
"Content": "<p>Your report is ready. <a href="https://contoso.com/reports">View it here</a>.</p>"
}Looks fine, right? The API didn't think so. Turns out JSON sees that quote right before https as the end of the Content value. Everything after it is gibberish to the parser, so the call came back with an error because it had no idea what I wanted.
The Fix: Escape Characters
After some quick research with Copilot, I landed on escape characters. In JSON, a backslash ( \ ) in front of a character tells the parser "this is part of the text, not part of the structure." So every quote inside the HTML gets a backslash in front of it:
{
"To": "someone@contoso.com",
"Subject": "Your report is ready",
"Content": "<p>Your report is ready. <a href=\"https://contoso.com/reports\">View it here</a>.</p>"
}Same content, one extra character per quote, and the API was happy. The backslashes don't show up in the final email either. They get consumed when the JSON is read, so the recipient just sees a normal link.
A Few Gotchas I Picked Up Along the Way
Only escape the quotes inside the value. The quotes wrapping "Content" and the start and end of its value stay as-is. Those are the ones JSON actually needs.
Quotes aren't the only troublemakers. A literal backslash needs to become \\, and a line break becomes \n. If you're pasting HTML in from somewhere with line breaks, that one will bite you.
Single quotes work too. HTML is fine with <a href='https://contoso.com/reports'>, and JSON doesn't care about single quotes inside a value, so no escaping needed. Don't feel like the backslash is the only way to do this.
Expressions play by different rules. Power Automate expressions wrap strings in single quotes, and the backslash doesn't escape anything there. To get a single quote inside an expression string, you double it ('').
That last one is worth a quick example, since it's where a lot of us go next. If you're building the link in a concat() with the single-quote HTML approach, it looks like this:
concat('<a href=''', variables('ReportURL'), '''>View it here</a>')Don't freak out at the triple quotes. The outer one opens or closes the string, and the doubled pair inside is the single quote that ends up in your HTML. Pay attention to which layer you're in: JSON wants backslashes, expressions want doubled single quotes.
Wrapping Up
This one was small, but it's the kind of small thing that can eat an afternoon when you first start mixing HTML, JSON, and expressions. Once you know the parser is just reading your quotes literally, the error messages start making a lot more sense. I hope this helps someone out there battling a similar scenario.
Try. Fail. Learn.




Comments