在Azure Logic应用中将JSON数组转换为JSON对象

问题描述

我正在使用Azure Logic应用,试图将JSON数组转换为JSON对象。 例如,如果我有一个数组为:

[
  {
    name: 'john'
    id: '1'
  },{
    name: 'Sarah'
    id: '2'
  },]

我希望将其作为输出

{
'1': 'john','2': 'Sarah'
}

解决方法

首次初始化Json结果:

enter image description here

然后在foreach lopp(TestArray是我们的数据数组)中添加带有表达式"addProperty(variables('JsonResult'),item()?['id'],item()?['name'])"的compose并使用compose的输出设置变量:

enter image description here

示例运行: enter image description here enter image description here

洛杉矶的代码:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#","actions": {
            "For_each": {
                "actions": {
                    "Add_property": {
                        "inputs": "@addProperty(variables('JsonResult'),item()?['name'])","runAfter": {},"type": "Compose"
                    },"Set_variable": {
                        "inputs": {
                            "name": "JsonResult","value": "@outputs('Add_property')"
                        },"runAfter": {
                            "Add_property": [
                                "Succeeded"
                            ]
                        },"type": "SetVariable"
                    }
                },"foreach": "@variables('TestArray')","runAfter": {
                    "Init_Json_Result": [
                        "Succeeded"
                    ]
                },"runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },"type": "Foreach"
            },"Init_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "TestArray","type": "array","value": [
                                {
                                    "id": "1","name": "john"
                                },{
                                    "id": "2","name": "sarah"
                                }
                            ]
                        }
                    ]
                },"type": "InitializeVariable"
            },"Init_Json_Result": {
                "inputs": {
                    "variables": [
                        {
                            "name": "JsonResult","type": "object","value": {}
                        }
                    ]
                },"runAfter": {
                    "Init_Array": [
                        "Succeeded"
                    ]
                },"Test_result": {
                "inputs": "@variables('JsonResult')","runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },"type": "Compose"
            }
        },"contentVersion": "1.0.0.0","outputs": {},"parameters": {},"triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },"kind": "Http","type": "Request"
            }
        }
    },"parameters": {}
}
,

如上面的评论中所述,将其发布为答案。您可以按照以下步骤来实现:

  1. 仅用于测试流程,添加Initialize变量操作: enter image description here

  2. 解析输入数组: enter image description here

  3. 然后,初始化空数组变量: enter image description here

  4. 然后,使用For Each控件,并在其中使用“附加到数组变量”操作: enter image description here

现在,您可以使用output变量了,该变量具有结果数组。

测试结果: enter image description here

此Logic App的JSON代码:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#","actions": {
            "For_each": {
                "actions": {
                    "Append_to_array_variable": {
                        "inputs": {
                            "name": "output","value": {
                                "@{items('For_each')['id']}": "@{items('For_each')['name']}"
                            }
                        },"type": "AppendToArrayVariable"
                    }
                },"foreach": "@body('Parse_JSON')","runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },"HTTP": {
                "inputs": {
                    "body": "@variables('output')","method": "POST","uri": "https://prod-33.westeurope.logic.azure.com:443/workflows/a656267dd18d46d2aa21e79e4012ba29/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=eBgZCaKgCDWiMWjm7JGNNb1-QyXbnT5AlVgBQt1GF48"
                },"type": "Http"
            },"Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "output","value": []
                        }
                    ]
                },"runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },"Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "input","Parse_JSON": {
                "inputs": {
                    "content": "@variables('input')","schema": {
                        "items": {
                            "properties": {
                                "id": {
                                    "type": "string"
                                },"name": {
                                    "type": "string"
                                }
                            },"required": [
                                "name","id"
                            ],"type": "object"
                        },"type": "array"
                    }
                },"runAfter": {
                    "Initialize_variable_2": [
                        "Succeeded"
                    ]
                },"type": "ParseJson"
            }
        },"parameters": {}
}