jsonadaptive-cardspower-automatemicrosoft-planner

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.


Solution

  • 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"
    }