首页 文章

Azure Logic App - JSON到XML转换

提问于
浏览
1

这看起来很明显但不知何故它不适合我 . 我正在尝试在Microsoft Azure上的Logic App中构建解决方案但我仍然坚持将JSON对象转换为XML .

我的要求是执行存储过程并以XML格式保存响应 . 默认情况下,SQL执行存储过程操作以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
      }]
 }
}

然后,在“创建Blob”操作中使用上面的响应来保存Azure上blob的响应 .

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

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

错误:模板语言函数'xml'参数无效 . 提供的值无法转换为XML:'This document already has a ' DocumentElement ' node.' . 有关使用详情,请参阅https://aka.ms/logicexpressions#xml .

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

错误:模板语言函数'xml'期望其参数是字符串或对象 . 提供的值的类型为'Array' . 有关使用详情,请参阅https://aka.ms/logicexpressions#xml .

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

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

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

2 回答

  • 1

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

    第一个示例在根级别具有多个元素,这是错误消息在“此文档已经具有'DocumentElement'节点”中抱怨的内容 .

    您的“正确”JSON确实只有一个根元素 .

  • 0

    发布问题后,我进一步分析了问题,发现我在@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 . 希望这有助于某人 .

相关问题