在逻辑应用程序中将 JSON 转换为 XML

Convert JSON to XML in Logic Apps

我已经使用解析 json 连接器在逻辑应用程序中解析了一个 JSON 字符串。如何将该步骤的输出放入 xml 文件中? 我看到的一个建议是使用 "when a http request is received",但这是一个触发器,我需要一个操作。 用户语音请求解析 xml 函数 here .我需要一个解决方法。 我遇到的另一个问题是 parse json 正在返回一个对象,但我想我需要一个数组。

这是我的解析 json 连接器中的架构。解析 json 之后,我正在尝试初始化一个变量。它不接受字符串类型。

<code>
{
"properties": {
    "PR_ADD1": {
        "type": "string"
    },
    "PR_ADD2": {
        "type": "string"
    },
    "PR_ADD3": {
        "type": "string"
    },
    "RLS_GROUP": {
        "type": "string"
    }
},
"title": "The Root Schema",
"type": "object"
}  
</code>

How do I initialize the first variable as string with the output from my 
previous parse json step?  In [this example] 
(https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-perform- 
data-operations#parse-json-action) there is only one record.  My parse 
json step has lots of records.

这是一些示例 JSON

<code>
 {
 "properties": {
    "PR_ADD1": {
        "type": "string"
    },
    "PR_ADD2": {
        "type": "string"
    },
    "PR_ADD3": {
        "type": "string"
    },
    "PR_ZONEC": {
        "type": "string"
    },
    "RLS_GROUP": {
        "type": "string"
    }
 },
 "title": "The Root Schema",
 "type": "object"
 }
</code>

这是我的 Logic APP

{
"definition": {
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "actions": {
        "Compose_2": {
            "inputs": "@length(variables('IntermediateArray'))",
            "runAfter": {
                "Scope": [
                    "Succeeded"
                ],
                "Set_variable": [
                    "Succeeded",
                    "Skipped"
                ]
            },
            "type": "compose"
        },
        "Compose_From_HTTP2": {
            "inputs": "@body('HTTP_2')",
            "runAfter": {
                "HTTP_2": [
                    "Succeeded"
                ]
            },
            "type": "compose"
        },
        "HTTP_2": {
            "inputs": {
                "body": {
                    "FORMAT": "payload",
                    "FROM": 0,
                    "GRIDID": "PROP",
                    "GRIDVIEW": "1",
                    "HITS": 100,
                    "ORDERBY": "PR_DATESOLD",
                    "PROFILE": [
                        {
                            "PR_NAME": "G*",
                            "PR_USER1": "GENERATED"
                        }
                    ],
                    "sessionID": "@body('Parse_JSON3')['sessionID']"
                },
                "headers": {
                    "Accept": "text/json",
                    "Content-Type": "text/json"
                },
                "method": "POST",
                "uri": "@variables('uri_DefGrid')"
            },
            "runAfter": {
                "Parse_JSON3": [
                    "Succeeded"
                ]
            },
            "type": "Http"
        },
        "HTTP_Logout": {
            "inputs": {
                "body": {
                    "method": "logout",
                    "sessionID": "@body('Parse_JSON3')['sessionID']"
                },
                "headers": {
                    "Accept": "text/json",
                    "Content-Type": "text/json"
                },
                "method": "POST",
                "uri": "@variables('uri_logout')"
            },
            "runAfter": {
                "Initialize_uri_logout": [
                    "Succeeded"
                ]
            },
            "type": "Http"
        },
        "Initialize_Header": {
            "inputs": {
                "variables": [
                    {
                        "name": "Header",
                        "type": "string",
                        "value": "{\"Accept\":\"text/json\",\"Content-Type\":\"text/json\"}"
                    }
                ]
            },
            "runAfter": {
                "Initialize_body_login": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable"
        },
        "Initialize_body_login": {
            "inputs": {
                "variables": [
                    {
                        "name": "body_login",
                        "type": "string",
                        "value": "json(@{triggerBody()})"
                    }
                ]
            },
            "runAfter": {},
            "type": "InitializeVariable"
        },
        "Initialize_intermediateArray": {
            "inputs": {
                "variables": [
                    {
                        "name": "intermediateArray",
                        "type": "array"
                    }
                ]
            },
            "runAfter": {
                "Compose_From_HTTP2": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable"
        },
        "Initialize_sharepointLibraryPath": {
            "inputs": {
                "variables": [
                    {
                        "name": "sharepointLibraryPath",
                        "type": "string",
                        "value": "https://qqqonline.sharepoint.com/teams-and-projects/dev/hhh"
                    }
                ]
            },
            "runAfter": {
                "Compose_2": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable"
        },
        "Initialize_uri_DefGrid": {
            "inputs": {
                "variables": [
                    {
                        "name": "uri_DefGrid",
                        "type": "string",
                        "value": "https://mh-uat.ttt-app.uk:443/qqq_uat/wrd/run/pppMHAPI.GRIDGET"
                    }
                ]
            },
            "runAfter": {
                "Initialize_uri_login": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable"
        },
        "Initialize_uri_login": {
            "inputs": {
                "variables": [
                    {
                        "name": "uri_login",
                        "type": "string",
                        "value": "https://mh-uat.ttt-app.uk:443/qqq_uat/wrd/run/pppJSONSERVICE.LOGIN"
                    }
                ]
            },
            "runAfter": {
                "Initialize_Header": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable"
        },
        "Initialize_uri_logout": {
            "inputs": {
                "variables": [
                    {
                        "name": "uri_logout",
                        "type": "string",
                        "value": "https://mh-uat.ttt-app.uk:443/qqq_uat/wrd/run/pppJSONSERVICE.LOGOUT"
                    }
                ]
            },
            "runAfter": {
                "Initialize_sharepointLibraryPath": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable"
        },
        "Initialize_variable": {
            "inputs": {
                "variables": [
                    {
                        "name": "initializeArray",
                        "type": "array",
                        "value": "@outputs('Compose_From_HTTP2')"
                    }
                ]
            },
            "runAfter": {
                "Initialize_intermediateArray": [
                    "Succeeded"
                ]
            },
            "type": "InitializeVariable"
        },
        "Parse_JSON3": {
            "inputs": {
                "content": "@triggerBody()",
                "schema": {
                    "properties": {
                        "RLS_WHERE": {
                            "$id": "#/properties/RLS_WHERE",
                            "type": "string"
                        },
                        "contact": {
                            "type": "string"
                        },
                        "error": {
                            "type": "string"
                        },
                        "errorId": {
                            "type": "string"
                        },
                        "fullName": {
                            "type": "string"
                        },
                        "labellanguage": {
                            "type": "string"
                        },
                        "language": {
                            "type": "string"
                        },
                        "message": {
                            "type": "string"
                        },
                        "params": {
                            "properties": {
                                "WOPARTSOPT": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "WOPARTSOPT"
                            ],
                            "title": "The Params Schema",
                            "type": "object"
                        },
                        "role": {
                            "type": "string"
                        },
                        "sessionID": {
                            "type": "string"
                        },
                        "success": {
                            "type": "string"
                        },
                        "userEmail": {
                            "$id": "#/properties/userEmail",
                            "type": "string"
                        }
                    },
                    "required": [
                        "success",
                        "message",
                        "sessionID",
                        "language",
                        "labellanguage",
                        "error",
                        "errorId",
                        "fullName",
                        "role",
                        "contact",
                        "RLS_WHERE",
                        "userEmail",
                        "params"
                    ],
                    "title": "The Root Schema",
                    "type": "object"
                }
            },
            "runAfter": {
                "Initialize_uri_DefGrid": [
                    "Succeeded"
                ]
            },
            "type": "ParseJson"
        },
        "Scope": {
            "actions": {
                "Scope_2": {
                    "actions": {
                        "Compose": {
                            "inputs": "@array(outputs('Compose_From_HTTP2'))",
                            "runAfter": {},
                            "type": "compose"
                        },
                        "Set_variable_2": {
                            "inputs": {
                                "name": "intermediateArray",
                                "value": "@outputs('Compose')"
                            },
                            "runAfter": {
                                "Compose": [
                                    "Succeeded"
                                ]
                            },
                            "type": "SetVariable"
                        }
                    },
                    "runAfter": {},
                    "type": "Scope"
                }
            },
            "runAfter": {
                "Initialize_variable": [
                    "Failed"
                ]
            },
            "type": "Scope"
        },
        "Set_variable": {
            "inputs": {
                "name": "intermediateArray",
                "value": "@outputs('Compose_From_HTTP2')"
            },
            "runAfter": {
                "Initialize_variable": [
                    "Succeeded"
                ]
            },
            "type": "SetVariable"
        }
    },
    "contentVersion": "1.0.0.0",
    "outputs": {},
    "parameters": {},
    "triggers": {
        "HTTP": {
            "inputs": {
                "body": {
                    "method": "login",
                    "password": "qqq_API",
                    "username": "qqq_API"
                },
                "headers": {
                    "Accept": "text/json",
                    "Content-Type": "text/json"
                },
                "method": "POST",
                "uri": "https://mh-uat.ttt-app.uk:443/qqq_uat/wrd/run/pppJSONSERVICE.LOGIN"
            },
            "recurrence": {
                "frequency": "Minute",
                "interval": 4
            },
            "type": "Http"
        }
    }
},
"parameters": {}
}

解析 json 屏幕截图显示了架构的顶部。这是底部。

"RLS_GROUP": {
        "type": "string"
    }
},
"title": "The Root Schema",
"type": "object"
}

当我在解析 json 中从样本输出生成模式时,我收到这样的错误消息。我认为当整数字段没有值时,它们会返回两个双引号来表示空而不是空。另外,我认为该架构基于可能包含数据的第一条记录。后续记录可能不会。相反,第一条记录可能没有特定字段的数据,但后续记录将有该字段的数据。

"message": "Invalid type. Expected Integer but got String.",
"lineNumber": 0,
"linePosition": 0,
"path": "Payload[1].PR_RENT",
"value": "",
"schemaId": "#/properties/Payload/items/properties/PR_RENT",
"errorType": "type",
"childErrors": []
},
{
"message": "Invalid type. Expected String but got Integer.",
"lineNumber": 0,
"linePosition": 0,
"path": "Payload[3].PR_ID",
"value": 1363,
"schemaId": "#/properties/Payload/items/properties/PR_ID",
"errorType": "type",
"childErrors": []
},
{
"message": "Invalid type. Expected String but got Integer.",
"lineNumber": 0,
"linePosition": 0,
"path": "Payload[3].PR_USER8",
"value": 0,
"schemaId": "#/properties/Payload/items/properties/PR_USER8",
"errorType": "type",
"childErrors": []
}

1. 对于你的第一个问题,我们可以在逻辑应用程序中使用 xml() 方法来进行转换。因为我不知道你的 json 数据(你在你的问题中提到它是一个 json 字符串),所以我假设你的数据类似于下面的数据:

[
  {
    "name": "Mike",
    "city": "ABC"
  },
  {
    "name": "Tom",
    "city": "DEF"
  }
]

然后我们就可以按照下面截图中的步骤进行转换了:

截图中的表达式为:

xml(json(concat('{"root":{"person":', variables('test'), '}}')))

之后,我们终于可以得到xml个数据了:

<root><person><name>Mike</name><city>ABC</city></person><person><name>Tom</name><city>DEF</city></person></root>

2.关于return对象或数组的第二个问题,这取决于您输入到"Parse JSON"操作的数据。

如果输入如下截图所示的数组数据

我们将从 "Parse JSON" 操作中得到一个数组 return

如果你输入如下截图所示的对象数据

我们将从 "Parse JSON" 操作中得到一个对象 return

希望对您的问题有所帮助~

更新:

根据您的意见,您想从 "Parse JSON" 中选择一些字段,然后将其转换为 xml。我post下面举个例子供大家参考:

我假设你的数据是这样的:

{
    "PR_ADD1": "aaa",
    "PR_ADD2": "bbb",
    "PR_ADD3": "ccc",
    "PR_ZONEC": "ddd",
    "RLS_GROUP": "eee"
}

并且在解析 json 之后,我假设您不想要 "PR_ZONEC" 字段并保留其他四个字段,并将它们转换为 xml。所以请参考我的逻辑应用:

  1. 我做了"Parse JSON"动作。

  2. 然后,使用"Compose"动作,在"Compose"动作中我删除字段"PR_ZONEC",保留其他四个字段。

  3. 最后转换为xml。

表达式为

xml(json(concat('{"root":', string(outputs('Compose')), '}')))
  1. 经过这些步骤,我得到了我想要的结果。

整个xml是

<root>
<PR_ADD1>aaa</PR_ADD1>
<PR_ADD2>bbb</PR_ADD2>
<PR_ADD3>ccc</PR_ADD3>
<RLS_GROUP>eee</RLS_GROUP>
</root>