Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stringify JSON in Logic App

We are sending messages to a service bus using a logic app. These messages will later be consumed by another service, the service expects the message content to be a string - essentially a stringified JSON object, with escape characters.

We are not able to find a method to stringify a JSON object in Logic Apps. Even if we explicitly provide a escaped string the logic app itself detects that it's stringified JSON and unescapes it and then sends it as a JSON object. We don't want that, we simply want it to send the string as it is. We have already tried changing the content type to text/plain, it does not work. The logic app always sends the unescaped string as JSON.

This post on MSDN: https://social.msdn.microsoft.com/Forums/office/en-US/e5dee958-09a7-4784-b1bf-facdd6b8a568/post-json-from-logic-app-how-to-escape-data?forum=azurelogicapps is of no help because doing this will violate the request contract of the message consuming service

like image 385
Pranshu Gupta Avatar asked Jun 22 '26 23:06

Pranshu Gupta


1 Answers

Do you need the stringified message to include opening and closing double quotes?

I've tried this and it worked for me.

  1. I have my JSON object as an output of a compose
  2. Then, I initialised a variable with the Base64 encoded value of the escaped stringified JSON (you need to add ALL the proper escaping required, mine was just a PoC)
  3. Then, you send the variable already in Base64 to Service Bus. (You need to remove the encoding on that action).

    "actions": {
        "Compose_JSON_Object": {
            "inputs": {
                "message": "I want this as a string"
            },
            "runAfter": {},
            "type": "Compose"
        },
        "Initialise_Variable_with_Stringified_JSON_Base64_Encoded": {
            "inputs": {
                "variables": [
                    {
                        "name": "jsonAsStringBase64",
                        "type": "String",
                        "value": "@base64(concat('\"', replace(string(outputs('Compose_JSON_Object')), '\"', '\\\"'), '\"'))"
                    }
                ]
            },
            "runAfter": {
                "Compose_JSON_Object": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable"
        },
        "Send_message": {
            "inputs": {
                "body": {
                    "ContentData": "@variables('jsonAsStringBase64')",
                    "ContentType": "text/plain"
                },
                "host": {
                    "connection": {
                        "name": "@parameters('$connections')['servicebus']['connectionId']"
                    }
                },
                "method": "post",
                "path": "/@{encodeURIComponent(encodeURIComponent('temp'))}/messages",
                "queries": {
                    "systemProperties": "None"
                }
            },
            "runAfter": {
                "Initialise_Variable_with_Stringified_JSON_Base64_Encoded": [
                    "Succeeded"
                ]
            },
            "type": "ApiConnection"
        }
    },
    

This way, I got the message stringified.

HTH

like image 126
Paco de la Cruz Avatar answered Jun 25 '26 21:06

Paco de la Cruz