将 SQL 存储过程结果集 table JSON 转换为 XML

Convert SQL Stored procedure ResultSet table JSON to XML

这看起来很明显,但不知何故对我不起作用。我正在尝试在 Microsoft Azure 上的逻辑应用程序中构建解决方案,但我无法将 JSON 对象转换为 XML。

我的要求是执行存储过程并以 XML 格式保存响应。默认情况下 SQL 执行存储过程操作 returns 以下 JSON 格式的响应,

    {
"OutputParameters": { },
"ReturnCode": 0,
"ResultSets": {
"Table1": [
      {
        "ProductID": 680,
        "Name": "HL Road Frame - Black, 58",
        "ProductNumber": "FR-R92B-58",
        "Color": "Black",
        "StandardCost": 1059.31,
        "ListPrice": 1431.5,
        "Size": "58",
        "Weight": 1016.04
      },
      {
        "ProductID": 706,
        "Name": "HL Road Frame - Red, 58",
        "ProductNumber": "FR-R92R-58",
        "Color": "Red",
        "StandardCost": 1059.31,
        "ListPrice": 1431.5,
        "Size": "58",
        "Weight": 1016.04
      }]
 }
}

然后在 "Create Blob" 操作中使用以上响应以将响应保存在 Azure 上的 blob 中。

这个 link 说逻辑应用程序提供了 xml 函数来将字符串或 JSON 对象转换为 XML 但这似乎没有按预期工作。我尝试了下面的表达式,但没有任何效果,

  1. @xml(正文('Execute_stored_procedure')?['ResultSets'])

错误:模板语言函数'xml'参数无效。提供的值无法转换为 XML:“此文档已有 'DocumentElement' 节点。”。请参阅 https://aka.ms/logicexpressions#xml 了解使用详情。

  1. @xml(正文('Execute_stored_procedure')?['ResultSets']['Table1'])

错误:模板语言函数'xml'要求其参数为字符串或对象。提供的值的类型为 'Array'。请参阅 https://aka.ms/logicexpressions#xml 了解使用详情。

我只想将此 JSON 转换为如下所示的 XML,

<Root><Product>....</Product><Product>....</Product></Root>

替代解决方案可能是调用 Azure 函数并将此 JSON 转换为 c# 代码中的 XML。但在我尝试替代解决方案之前,我想知道我做错了什么。

发布问题后,我进一步分析了问题,发现我在@xml 函数中传递了错误的 JSON 对象。

正确的 JSON 对象应该如下所示,

{
"ResultSets": {
"Table1": [
      {
        "ProductID": 680,
        "Name": "HL Road Frame - Black, 58",
        "ProductNumber": "FR-R92B-58",
        "Color": "Black",
        "StandardCost": 1059.31,
        "ListPrice": 1431.5,
        "Size": "58",
        "Weight": 1016.04
      },
      {
        "ProductID": 706,
        "Name": "HL Road Frame - Red, 58",
        "ProductNumber": "FR-R92R-58",
        "Color": "Red",
        "StandardCost": 1059.31,
        "ListPrice": 1431.5,
        "Size": "58",
        "Weight": 1016.04
      }]
 }
}

请注意,我必须删除以下几行,

"OutputParameters": { },
"ReturnCode": 0,

所以尝试了下面的表达式并且成功了,

@xml(json(concat('{\"ResultSets\":',body('Execute_stored_procedure').ResultSets,'}')))

现在我需要稍微调整这个表达式以获得最终的 XML。希望这对某人有所帮助。

为了转换为 XML,JSON 需要有一个根元素。

第一个示例在根级别有多个元素,这就是 "This document already has a 'DocumentElement' node" 中的错误消息所抱怨的内容。

您的 'correct' JSON 确实有一个根元素。