Gerrit
Gerrit

Reputation: 200

How to escape quotes in json within Microsoft Flow?

I have a MS Flow to built an Adaptive Card out of a freshly generated Planner task. It works unless a title has double quotes in it. This breaks the json-code for the Adaptive Card.

I tried to inject the @replace-function directly into the json-code, but it didn't work.

This is my working (without quotes in the title of the task) example:

{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "text": "@{triggerBody()?['title']}",
            "wrap": true
        },
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.0"
}

This is what I tried with replace:

{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "text": "@{replace(@{triggerBody()?['title']},'\"','\\\"'}",
            "wrap": true
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.0"
}

I hoped the replace function would replace the double quotes (") with an escaped blackslash-version (\").

But the error in MS Flow says: Unable to get property 'properties' of undefined or null reference.

Upvotes: 1

Views: 9834

Answers (2)

Gerrit
Gerrit

Reputation: 200

I finally found the errors in my approach:

  1. I tried a nested reference (two times @{})
  2. I quoted the nested double quotes wrong

So here is the solution:

{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "text": "@{replace(triggerBody()?['title'],'"','\"')}",
            "wrap": true
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.0"
}

Upvotes: 5

Gerrit
Gerrit

Reputation: 200

I found something I would consider a workaround. But, still have the feeling, there must be a better solution. I added a step in between to initialise my own variable, and did put the replace-function in there:

screenshot of replace

So the function comes down to: replace(triggerBody()?['title'],'"','\"')

Upvotes: 2

Related Questions