将 JSON 数组转换为 Azure 逻辑应用程序中的 JSON 对象

Transform JSON array to JSON object in Azure Logic Apps

使用 Azure 逻辑应用程序,我正在尝试将 JSON 数组转换为 JSON 对象。 例如,如果我有一个数组:

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

我想要输出:

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

第一次初始化Json结果:

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

示例运行:

洛杉矶代码:

{
    "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()?['id'],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"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Init_Json_Result": {
                "inputs": {
                    "variables": [
                        {
                            "name": "JsonResult",
                            "type": "object",
                            "value": {}
                        }
                    ]
                },
                "runAfter": {
                    "Init_Array": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "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. 为了测试流程,添加初始化变量动作:

  2. 解析输入数组:

  3. 然后,初始化空数组变量:

  4. 然后,使用 For Each 控件并在其中可以使用 Append to array variable 操作:

现在,您可以使用 output 变量,其中包含您的结果数组。

测试结果:

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']}"
                            }
                        },
                        "runAfter": {},
                        "type": "AppendToArrayVariable"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "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"
                },
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Http"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "output",
                            "type": "array",
                            "value": []
                        }
                    ]
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "input",
                            "type": "array",
                            "value": [
                                {
                                    "id": "1",
                                    "name": "john"
                                },
                                {
                                    "id": "2",
                                    "name": "sarah"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "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"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}