Azure 逻辑应用程序从单独收到的 xml 创建 JSON 数组

Azure Logic Apps create JSON array from separate received xml

我是 Azure 新手,必须根据逻辑应用收到的单独 XML 消息创建 JSON 数组。逻辑应用请求数据并接收 XML 格式的响应。我提出了一种将消息保存在 Azure 存储中然后通过 Azure 函数创建 JSON 数组的方法。这种方法会影响性能吗?有什么想法吗?

提前致谢

有两种方法可以完全依赖逻辑应用程序而不涉及 Azure 功能

将此作为样本 xml

<?xml version="1.0"?>
<PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
  <Address Type="Shipping">
    <Name>Adam Murphy</Name>
    <Street>123 Maple Street</Street>
    <City>Mill Valley</City>
    <State>CA</State>
    <Zip>10999</Zip>
    <Country>Ireland</Country>
  </Address>
  <Address Type="Billing">
    <Name>Tai Yee</Name>
    <Street>8 Oak Avenue</Street>
    <City>Old Town</City>
    <State>PA</State>
    <Zip>95819</Zip>
    <Country>Ireland</Country>
  </Address>
  <DeliveryNotes />
</PurchaseOrder>

方式一

您可以使用 json(xml(triggerBody())) 直接将 xml 转换为 json 这是供您参考的逻辑应用程序 -

输出

如果您想要自定义 json,则可以使用 Parse_JsonCompose 连接器创建一个。

这里我只是将 Compose 连接器的输出带到 Parse_Json 以解析结果 Json 以获得自定义 JSON 脚本。下面是我正在尝试创建的 Json。

{
  "PurchaseOrder": {
    "OrderNumber": "@{body('Parse_JSON')?['PurchaseOrder']?['@PurchaseOrderNumber']}",
    "PurchaseDate": "@{body('Parse_JSON')?['PurchaseOrder']?['@OrderDate']}",
    "Location": [
      {
        "Name": "@{items('For_each')?['Name']}",
        "Address": "@{items('For_each')?['Street']},@{items('For_each')?['City']},@{items('For_each')?['State']},@{items('For_each')?['Country']},@{items('For_each')?['Zip']}"
      }
    ]
  }
}

输出

这是我的逻辑应用程序的代码视图。您可以直接使用它在您的逻辑应用程序中获取准确的工作流程。

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@json(xml(triggerBody()))",
                "runAfter": {},
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Compose_2": {
                        "inputs": {
                            "PurchaseOrder": {
                                "Location": [
                                    {
                                        "Address": "@{items('For_each')?['Street']},@{items('For_each')?['City']},@{items('For_each')?['State']},@{items('For_each')?['Country']},@{items('For_each')?['Zip']}",
                                        "Name": "@{items('For_each')?['Name']}"
                                    }
                                ],
                                "OrderNumber": "@{body('Parse_JSON')?['PurchaseOrder']?['@PurchaseOrderNumber']}",
                                "PurchaseDate": "@{body('Parse_JSON')?['PurchaseOrder']?['@OrderDate']}"
                            }
                        },
                        "runAfter": {},
                        "type": "Compose"
                    }
                },
                "foreach": "@body('Parse_JSON')?['PurchaseOrder']?['Address']",
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@outputs('Compose')",
                    "schema": {
                        "properties": {
                            "?xml": {
                                "properties": {
                                    "@@version": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "PurchaseOrder": {
                                "properties": {
                                    "@@OrderDate": {
                                        "type": "string"
                                    },
                                    "@@PurchaseOrderNumber": {
                                        "type": "string"
                                    },
                                    "Address": {
                                        "items": {
                                            "properties": {
                                                "@@Type": {
                                                    "type": "string"
                                                },
                                                "City": {
                                                    "type": "string"
                                                },
                                                "Country": {
                                                    "type": "string"
                                                },
                                                "Name": {
                                                    "type": "string"
                                                },
                                                "State": {
                                                    "type": "string"
                                                },
                                                "Street": {
                                                    "type": "string"
                                                },
                                                "Zip": {
                                                    "type": "string"
                                                }
                                            },
                                            "required": [
                                                "@@Type",
                                                "Name",
                                                "Street",
                                                "City",
                                                "State",
                                                "Zip",
                                                "Country"
                                            ],
                                            "type": "object"
                                        },
                                        "type": "array"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

方式二

您可以使用液态模板,以便使用 Transform XML to JSON 连接器将 xml 转换为 json。更多信息可以参考 and .

参考资料: Logic App: Basic XML to JSON Expression Conversion