0

I have a for each action that receives a JSON array and adds an Item:Key pair.

The input for the for each looks like this:

[
  {
    "memberName": "G-TEL FOR ENBRIDGE GAS (LEGACY UNION GAS) (ENOW01)",
    "stationCode": "ENOW01",
    "initialStatus": "Notification sent"
  },
  {
    "memberName": "G-TEL FOR CITY OF BRANTFORD - STREETLIGHTS (BRASL01)",
    "stationCode": "BRASL01",
    "initialStatus": "Notification sent"
  },
  {
    "memberName": "G-TEL FOR BRANTFORD POWER (LOCAL HYDRO) (BRAP01)",
    "stationCode": "BRAP01",
    "initialStatus": "Notification sent"
  },
  {
    "memberName": "G-TEL FOR CITY OF BRANTFORD - SEWER/WATER/STORM (BRAS01)",
    "stationCode": "BRAS01",
    "initialStatus": "Cleared"
  },
  {
    "memberName": "G-TEL FOR BELL CANADA (BCOW01)",
    "stationCode": "BCOW01",
    "initialStatus": "Notification sent"
  }
]

I use this in a compose to insert a value from a variable: array(union(items('For_each_3'),outputs('Compose_8'))) and get this result:

[
  {
    "memberName": "G-TEL FOR ENBRIDGE GAS (LEGACY UNION GAS) (ENOW01)",
    "stationCode": "ENOW01",
    "initialStatus": "Notification sent",
    "LocateNumber": "2022054487"
  }
]

The same value is inserted into each object. Now I need to combine the results in order to add as rows in an Excel spreadsheet.

I am figuring things out as I go and I was trying to use this expression I found elsewhere to combine the results back into an array: join(items('For_each_3'),','). But I get an error that Join is expecting the first parameter to be an array. I thought that each result was already an array. I have hunted for an answer but can't find one.

As I mentioned, I want the output going to an excel spreadsheet so if there is a better way to do this, I would be open to hear it but I would still like to know how to join the results just for my own knowledge as well. Thanks.

DryBSMT
  • 51
  • 2
  • 12

2 Answers2

1

Here is one of the workaround. As you want to save the data to your excel I have directly used excel for business connector for instance (You can use any excel related connectors as per your requirements).

Here is my logic app

enter image description here

The output:-

enter image description here

Here is my generate excel file

enter image description here

SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
  • I was able to get this working perfectly, thank you. I am still working on the other solution. I am just uncertain of the etiquette for accepting an answer. The other solution directly answers my question (once I get it to work), whereas your solution gets me to where I was trying to get for my next step after getting the direct answer. – DryBSMT Jan 28 '22 at 23:44
  • @DryBSMT, you should select this one as the answer, it may not answer your direct question but it solves the larger picture. It's fine to make it the answer. – Skin Jan 29 '22 at 03:38
1

If I understand correctly, you want to add the same "LocateNumber" to each item in your original array, if that's the case, I managed to get something to work for you BUT, it does require a specific setting to be in place to work.

I created a new array object and then added each updated/new item into that new array. The Excel side of it, I haven't tackled but this answers the direct question regarding the issue you have.

The easiest way to show you is to give you the JSON definition so you can recreate it in your own tenant.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_Each_Array_Item": {
                "actions": {
                    "Append_Updated_Array_Item_Object_to_New_JSON_Array": {
                        "inputs": {
                            "name": "Updated JSON Array",
                            "value": "@variables('New Array Item Object')"
                        },
                        "runAfter": {
                            "Update_New_Array_Item_Object": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToArrayVariable"
                    },
                    "Set_Array_Item_Object": {
                        "inputs": {
                            "name": "Array Item Object",
                            "value": "@items('For_Each_Array_Item')"
                        },
                        "runAfter": {},
                        "type": "SetVariable"
                    },
                    "Update_New_Array_Item_Object": {
                        "inputs": {
                            "name": "New Array Item Object",
                            "value": "@addProperty(variables('Array Item Object'), 'LocateNumber', '2022054487')"
                        },
                        "runAfter": {
                            "Set_Array_Item_Object": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    }
                },
                "foreach": "@variables('JSON')",
                "runAfter": {
                    "Initialize_Updated_JSON_Array": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Initialize_Array_Item_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Array Item Object",
                            "type": "object"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_JSON_Array": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Debug_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Debug Array",
                            "type": "array",
                            "value": "@variables('Updated JSON Array')"
                        }
                    ]
                },
                "runAfter": {
                    "For_Each_Array_Item": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_JSON_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "JSON",
                            "type": "array",
                            "value": [
                                {
                                    "initialStatus": "Notification sent",
                                    "memberName": "G-TEL FOR ENBRIDGE GAS (LEGACY UNION GAS) (ENOW01)",
                                    "stationCode": "ENOW01"
                                },
                                {
                                    "initialStatus": "Notification sent",
                                    "memberName": "G-TEL FOR CITY OF BRANTFORD - STREETLIGHTS (BRASL01)",
                                    "stationCode": "BRASL01"
                                },
                                {
                                    "initialStatus": "Notification sent",
                                    "memberName": "G-TEL FOR BRANTFORD POWER (LOCAL HYDRO) (BRAP01)",
                                    "stationCode": "BRAP01"
                                },
                                {
                                    "initialStatus": "Cleared",
                                    "memberName": "G-TEL FOR CITY OF BRANTFORD - SEWER/WATER/STORM (BRAS01)",
                                    "stationCode": "BRAS01"
                                },
                                {
                                    "initialStatus": "Notification sent",
                                    "memberName": "G-TEL FOR BELL CANADA (BCOW01)",
                                    "stationCode": "BCOW01"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_New_Array_Item_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "New Array Item Object",
                            "type": "object"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Array_Item_Object": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Updated_JSON_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Updated JSON Array",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_New_Array_Item_Object": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "recurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {}
}

The basis of the flow is ...

  1. Create variables that store the existing array, the array item being processed, the new/updated item and then the new array.
  2. Process each item in the array, store the current item in a new variable (have included this for debugging/visibility), then update that item with the new property and finally, add it to the new array.

ForEach

The expression in that formula (although found in the attached definition) looks like this ...

addProperty(variables('Array Item Object'), 'LocateNumber', '2022054487')

Critical Setting

The For Each must have concurrency switched to 1. It didn't like having it on, it kept coming up with the wrong output in the new array where items were duplicated.

Go to to the Settings on the action and make it look like the below.

Concurrency

The last step in the flow is just a new variable which helps visualise the output after the flow runs.

[
  {
    "initialStatus": "Notification sent",
    "memberName": "G-TEL FOR ENBRIDGE GAS (LEGACY UNION GAS) (ENOW01)",
    "stationCode": "ENOW01",
    "LocateNumber": "2022054487"
  },
  {
    "initialStatus": "Notification sent",
    "memberName": "G-TEL FOR CITY OF BRANTFORD - STREETLIGHTS (BRASL01)",
    "stationCode": "BRASL01",
    "LocateNumber": "2022054487"
  },
  {
    "initialStatus": "Notification sent",
    "memberName": "G-TEL FOR BRANTFORD POWER (LOCAL HYDRO) (BRAP01)",
    "stationCode": "BRAP01",
    "LocateNumber": "2022054487"
  },
  {
    "initialStatus": "Cleared",
    "memberName": "G-TEL FOR CITY OF BRANTFORD - SEWER/WATER/STORM (BRAS01)",
    "stationCode": "BRAS01",
    "LocateNumber": "2022054487"
  },
  {
    "initialStatus": "Notification sent",
    "memberName": "G-TEL FOR BELL CANADA (BCOW01)",
    "stationCode": "BCOW01",
    "LocateNumber": "2022054487"
  }
]

I trust you can adapt this into your own flow if you think that solution is acceptable.

Skin
  • 9,085
  • 2
  • 13
  • 29
  • I was able to get this to work, somewhat. I was only able to get a single object instead of an array. Can you clarify what type of variables to initiate and then which variable actions to use. I am struggling to get them matched up correctly. Thanks. – DryBSMT Jan 26 '22 at 18:04
  • The best thing to do is to create a new logic app and then paste in the definition I attached in code view. So when you’re in the logic app designer, there’s a button at the top called code view. You should be able to take that definition and paste it in, it will then give you a working version of my answer. – Skin Jan 26 '22 at 20:17
  • I was able to use the code as you suggested and it seems to work. The only part that does not work is the debug for viewing the results because I have to initialize it right at the top of the flow. I can't do it below the `For each`. Any thoughts on how I can see the output? – DryBSMT Jan 28 '22 at 23:38