Home > OS >  Get specific value from json in Logic App
Get specific value from json in Logic App

Time:11-02

I am handling error by using scopes in my Logic App. In case of an error I get the result of the scope which has thrown the error.

The problem is that the result contains all results of all steps contained in the relevant scope. This is looking like this (shortened version):

{
    "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\".."
            }
        }
    ]
}

I want to return a response with only the failed step. So I need to get the "message" of the step with the status "Failed".

How can I retrieve this message from the json? I know I have to parse the result first to get a "user friendly" json representation but now I am stuck with getting the message from the step which has failed.

In this example this would be this one:

{
            "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\".."
            }
        }

Any advice is higly appreciated.

CodePudding user response:

You will need to loop through each of the items in the "body". First parse each item and check if the "status" is = "Failed". If yes, then append it to a new array variable and return that result. This is shown in the attached picture.

enter image description here

The JSON code is below.

{
    "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": {}
}

CodePudding user response:

It seems to be a perfect example where the enter image description here

Result:

enter image description here

  • Related