从逻辑应用中的 json 获取特定值

Get specific value from json in Logic App

我正在通过在我的逻辑应用程序中使用范围来处理错误。如果出现错误,我会得到引发错误的范围的结果。

问题是结果包含相关范围内所有步骤的所有结果。这看起来像这样(简化版):

{
    "statusCode": "500",
    "body": [
        {
            "name": "Compose_Final",
            "startTime": "2021-10-30T07:05:25.5184987Z",
            "endTime": "2021-10-30T07:05:25.5341199Z",
            "trackingId": "104822d9-2680-4917-bde4-b09132e32de5",
            "clientTrackingId": "08585660294078874488209628375CU22",
            "code": "ActionSkipped",
            "status": "Skipped",
            "error": {
                "code": "ActionConditionFailed",
                "message": "Die Ausführung der Vorlagenaktion \"Compose_Final\" wird übersprungen: Die runAfter-Bedingung \"Set_variable\" wird nicht erfüllt: Erwartete Statuswerte: Succeeded. Tatsächlicher Wert: Failed."
            }
        },
        {
            "name": "For_each_Gehaltslauf",
            "inputs": {
                "foreachItems": [
                    "e80796aa-b3e0-eb11-bacb-000d3abb215c"
                ]
            },
            "inputsMetadata": {
                "foreachItemsCount": 1
            },
            "startTime": "2021-10-30T07:04:38.3305884Z",
            "endTime": "2021-10-30T07:05:25.4716195Z",
            "trackingId": "3e23002a-d3ee-4696-b05c-c6ba89403f36",
            "clientTrackingId": "08585660294078874488209628375CU22",
            "status": "Succeeded"
        },
        {
            "name": "Send_an_email_(V2)",
            "startTime": "2021-10-30T07:05:25.5497465Z",
            "endTime": "2021-10-30T07:05:25.5497465Z",
            "trackingId": "8d60b137-3094-4b79-b099-04ce50bb3875",
            "clientTrackingId": "08585660294078874488209628375CU22",
            "code": "ActionSkipped",
            "status": "Skipped",
            "error": {
                "code": "ActionConditionFailed",
                "message": "Die Ausführung der Vorlagenaktion \"Send_an_email_(V2)\" wird übersprungen: Die runAfter-Bedingung \"Compose_Final\" wird nicht erfüllt: Erwartete Statuswerte: Succeeded. Tatsächlicher Wert: Skipped."
            }
        },
        {
            "name": "Set_variable",
            "startTime": "2021-10-30T07:05:25.5028731Z",
            "endTime": "2021-10-30T07:05:25.5028731Z",
            "trackingId": "71ee5d61-8029-4a35-ab52-2b80fef031dd",
            "clientTrackingId": "08585660294078874488209628375CU22",
            "code": "BadRequest",
            "status": "Failed",
            "error": {
                "code": "InvalidTemplate",
                "message": "Vorlagensprachausdrücke in den Eingaben der Aktion \"Set_variable\" in Zeile \"1\" und Spalte \"2098\" können nicht verarbeitet werden: Versuch zum Teilen eines Integral- oder Dezimalwerts durch null in Funktion \"div\".."
            }
        }
    ]
}

我想 return 只包含失败步骤的响应。所以我需要获取状态为“失败”的步骤的“消息”。

如何从 json 检索此消息?我知道我必须首先解析结果以获得“用户友好”json 表示,但现在我无法从失败的步骤中获取消息。

在这个例子中就是这个:

{
            "name": "Set_variable",
            "startTime": "2021-10-30T07:05:25.5028731Z",
            "endTime": "2021-10-30T07:05:25.5028731Z",
            "trackingId": "71ee5d61-8029-4a35-ab52-2b80fef031dd",
            "clientTrackingId": "08585660294078874488209628375CU22",
            "code": "BadRequest",
            "status": "Failed",
            "error": {
                "code": "InvalidTemplate",
                "message": "Vorlagensprachausdrücke in den Eingaben der Aktion \"Set_variable\" in Zeile \"1\" und Spalte \"2098\" können nicht verarbeitet werden: Versuch zum Teilen eines Integral- oder Dezimalwerts durch null in Funktion \"div\".."
            }
        }

非常感谢任何建议。

您需要遍历“正文”中的每一项。首先解析每个项目并检查“状态”是否为=“失败”。如果是,则将其附加到一个新的数组变量和 return 结果。这在附图中显示。

JSON代码如下。

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Condition_2": {
                        "actions": {
                            "Append_to_array_variable": {
                                "inputs": {
                                    "name": "finalresult",
                                    "value": "@items('For_each')"
                                },
                                "runAfter": {},
                                "type": "AppendToArrayVariable"
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@body('Parse_JSON')?['status']",
                                        "Failed"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {
                            "Parse_JSON": [
                                "Succeeded"
                            ]
                        },
                        "type": "If"
                    },
                    "Parse_JSON": {
                        "inputs": {
                            "content": "@items('For_each')",
                            "schema": {
                                "properties": {
                                    "clientTrackingId": {
                                        "type": "string"
                                    },
                                    "code": {
                                        "type": "string"
                                    },
                                    "endTime": {
                                        "type": "string"
                                    },
                                    "error": {
                                        "properties": {
                                            "code": {
                                                "type": "string"
                                            },
                                            "message": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "name": {
                                        "type": "string"
                                    },
                                    "startTime": {
                                        "type": "string"
                                    },
                                    "status": {
                                        "type": "string"
                                    },
                                    "trackingId": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "runAfter": {},
                        "type": "ParseJson"
                    }
                },
                "foreach": "@triggerBody()?['body']",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "finalresult",
                            "type": "array",
                            "value": []
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Response": {
                "inputs": {
                    "body": "@variables('finalresult')",
                    "statusCode": 200
                },
                "kind": "Http",
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Response"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {
                        "properties": {
                            "body": {
                                "items": {
                                    "properties": {
                                        "clientTrackingId": {
                                            "type": "string"
                                        },
                                        "code": {
                                            "type": "string"
                                        },
                                        "endTime": {
                                            "type": "string"
                                        },
                                        "error": {
                                            "properties": {
                                                "code": {
                                                    "type": "string"
                                                },
                                                "message": {
                                                    "type": "string"
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "inputs": {
                                            "properties": {
                                                "foreachItems": {
                                                    "items": {
                                                        "type": "string"
                                                    },
                                                    "type": "array"
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "inputsMetadata": {
                                            "properties": {
                                                "foreachItemsCount": {
                                                    "type": "integer"
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "name": {
                                            "type": "string"
                                        },
                                        "startTime": {
                                            "type": "string"
                                        },
                                        "status": {
                                            "type": "string"
                                        },
                                        "trackingId": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "name",
                                        "startTime",
                                        "endTime",
                                        "trackingId",
                                        "clientTrackingId",
                                        "status"
                                    ],
                                    "type": "object"
                                },
                                "type": "array"
                            },
                            "statusCode": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

这似乎是应该使用 Filter array 操作的完美示例:

结果: