Home > Software design >  How to implement this kind of condition in Azure Logic App?
How to implement this kind of condition in Azure Logic App?

Time:04-30

I have the following json output structure in Azure Logic App using the Parse JSON activity:

[
{ "name": "John",
"working days":['monday', 'tuesday', 'friday'],
"starting hour": ['8 A.M'., '7 A.M', '8 A.M.']
},
{ "name": "Carl",
"working days":['monday', 'tuesday'],
"starting hour": ['6 A.M'., '6 A.M']
},
{ "name": "Claire",
"working days":['monday', 'wednesday', 'friday'],
"starting hour": ['8 A.M'., '6 A.M', '9 A.M.']
},
{ "name": "Lisa",
"working days":['monday', 'thursday','saturday'],
"starting hour": ['8 A.M'., '7 A.M', '8 A.M.']
}
]

I would like to loop through each user and add a condition, where "working days"equals tuesday it shall return the matching "starting hour". For example 7 A.M. in case for John and 6 A.M. for Carl.

I have already added a For Each Activity after the Parse JSON to iterate through the json to get each person, added another For Each afterwards to go through working days. Then I have added a Condition, to match if it is tuesday. If yes, then it returns 'true' as a boolean value. But how can I make it return the matching starting hour?

Maybe the picture helps to visualize. Below, working days and tuesday are just gap fillers.

enter image description here

Thanks and best regards!

Edit: same amount of values in arrays, delete wrongfully placed quotes

CodePudding user response:

It's quite difficult to take you through my entire answer to your question but this is the JSON definition (you can load this into your own tenant for testing and to see how it works) to my logic app that will give you the approach I would take ...

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_Each_Name": {
                "actions": {
                    "Reset_Starting_Hour": {
                        "inputs": {
                            "name": "Starting Hour",
                            "value": "@{string('')}"
                        },
                        "runAfter": {
                            "Set_Index_=_0": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    },
                    "Set_Index_=_0": {
                        "inputs": {
                            "name": "Index",
                            "value": 0
                        },
                        "runAfter": {
                            "Set_Working_Days_Array_Length": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    },
                    "Set_Working_Days_Array_Length": {
                        "inputs": {
                            "name": "Working Days Array Length",
                            "value": "@length(items('For_Each_Name')?['working days'])"
                        },
                        "runAfter": {},
                        "type": "SetVariable"
                    },
                    "Until_Index_=_Working_Days_Array_Length": {
                        "actions": {
                            "Condition": {
                                "actions": {
                                    "Set_Starting_Hour": {
                                        "inputs": {
                                            "name": "Starting Hour",
                                            "value": "@{items('For_Each_Name')?['starting hour'][variables('Index')]}"
                                        },
                                        "runAfter": {},
                                        "type": "SetVariable"
                                    }
                                },
                                "expression": {
                                    "and": [
                                        {
                                            "equals": [
                                                "@items('For_Each_Name')?['working days'][variables('Index')]",
                                                "tuesday"
                                            ]
                                        }
                                    ]
                                },
                                "runAfter": {},
                                "type": "If"
                            },
                            "Increment_Index": {
                                "inputs": {
                                    "name": "Index",
                                    "value": 1
                                },
                                "runAfter": {
                                    "Condition": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "IncrementVariable"
                            }
                        },
                        "expression": "@equals(variables('Index'), variables('Working Days Array Length'))",
                        "limit": {
                            "count": 10,
                            "timeout": "PT1H"
                        },
                        "runAfter": {
                            "Reset_Starting_Hour": [
                                "Succeeded"
                            ]
                        },
                        "type": "Until"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Initialize_Index": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Initialize_Index": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Index",
                            "type": "integer"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Starting_Hour": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Starting_Hour": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Starting Hour",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Working_Days_Array_Length": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Working_Days_Array_Length": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Working Days Array Length",
                            "type": "integer"
                        }
                    ]
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": [
                        {
                            "name": "John",
                            "starting hour": [
                                "8 A.M.",
                                "7 A.M.",
                                "8 A.M."
                            ],
                            "working days": [
                                "monday",
                                "tuesday",
                                "friday"
                            ]
                        },
                        {
                            "name": "Carl",
                            "starting hour": [
                                "6 A.M.",
                                "6 A.M."
                            ],
                            "working days": [
                                "monday",
                                "tuesday"
                            ]
                        },
                        {
                            "name": "Claire",
                            "starting hour": [
                                "8 A.M.",
                                "6 A.M.",
                                "9 A.M"
                            ],
                            "working days": [
                                "monday",
                                "wednesday",
                                "friday"
                            ]
                        },
                        {
                            "name": "Lisa",
                            "starting hour": [
                                "8 A.M.",
                                "7 A.M.",
                                "8 A.M."
                            ],
                            "working days": [
                                "monday",
                                "thursday",
                                "saturday"
                            ]
                        }
                    ],
                    "schema": {
                        "items": {
                            "properties": {
                                "name": {
                                    "type": "string"
                                },
                                "starting hour": {
                                    "items": {
                                        "type": "string"
                                    },
                                    "type": "array"
                                },
                                "working days": {
                                    "items": {
                                        "type": "string"
                                    },
                                    "type": "array"
                                }
                            },
                            "required": [
                                "name",
                                "working days",
                                "starting hour"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {},
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Month",
                    "interval": 12
                },
                "recurrence": {
                    "frequency": "Month",
                    "interval": 12
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {}
}

The premise of my answer is to determine how many items there are in each working days array and from there, it will loop through (all the while incrementing an index variable) and if it finds tuesday as a working day, it will get the associated starting hour value from the same index.

Note: each array should be the same length, if they're not, there would be potential for problems.

One thing to point out as well, this will only work if the For Each Name action has its concurrency set to 1 ...

Concurrency Control

If you don't know where that is, you'll find it in the settings of the action itself.

  • Related