1

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

AzUser1
  • 183
  • 1
  • 14
  • How would it handle the last scenario where the number of items in the two arrays is different, or, is that a dud example? Also, your json looks a little screwy with the quotes at the bottom there. – Skin Apr 30 '22 at 08:48
  • I don't think the number of items in both arrays will be different. I have taken the data from a pandas df. Therefore the number in the arrays will match. There might be a null value in the array but then the null value will also be in both arrays. – AzUser1 Apr 30 '22 at 08:53
  • Can you fix it up then? – Skin Apr 30 '22 at 08:53
  • 1
    Oh yeah, I haven't noticed but I changed it now. Thanks! Also deleted the quotes before the array – AzUser1 Apr 30 '22 at 08:56

1 Answers1

1

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.

Skin
  • 9,085
  • 2
  • 13
  • 29
  • Thanks, it worked! Although I'm curious how it will work with a bigger dataset but I will try it out – AzUser1 Apr 30 '22 at 12:46
  • Yeah, one of the rules of LogicApps is to avoid heavy amounts of looping. If you want to make it really performant, a Azure Function can help a lot. See how you go though. – Skin Apr 30 '22 at 13:16