在Logic App中对JSON进行Stringify

问题描述 投票:2回答:1

我们使用逻辑应用程序向服务总线发送消息。这些消息稍后将被另一个服务使用,该服务期望消息内容是一个字符串 - 本质上是一个字符串化的JSON对象,带有转义字符。

我们无法找到在Logic Apps中对JSON对象进行字符串化的方法。即使我们明确提供了一个转义字符串,逻辑应用程序本身也会检测到它是字符串化的JSON并对它进行unescape,然后将其作为JSON对象发送。我们不希望这样,我们只是希望它按原样发送字符串。我们已经尝试将内容类型更改为text / plain,但它不起作用。逻辑应用程序始终将未转义的字符串作为JSON发送。

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没有任何帮助,因为这样做会违反消息服务的请求合同

json azure azure-logic-apps stringify azure-servicebus-topics
1个回答
1
投票

您是否需要字符串化消息以包括打开和关闭双引号?

我试过这个,它对我有用。

  1. 我将我的JSON对象作为compose的输出
  2. 然后,我用转义的字符串化JSON的Base64编码值初始化了一个变量(你需要添加所有正确的转义,我的只是一个PoC)
  3. 然后,将已在Base64中的变量发送到Service Bus。 (您需要删除该操作的编码)。 "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" } },

这样,我得到了字符串化的消息。

HTH

© www.soinside.com 2019 - 2024. All rights reserved.