Santosh Gadhave
Santosh Gadhave

Reputation: 37

How to construct a JSON when [keys] are seperated from [values] inside Logic App

I have been struggling with this for a long time to construct an output where my [key] is separated from my array value, please help to share your expertise.

Below is the response, i have received from Azure Alert and I'm looking forward to create an output (refer Expected Output image).

{
    "tables": [
      {
        "columns": [
          {
            "name": "task",
            "type": "string"
          },
          {
            "name": "environment",
            "type": "string"
          },
          {
            "name": "workspace",
            "type": "string"
          }
        ],
        "name": "PrimaryResult",
        "rows": [
          [
            "job_name_1",
            "PRODUCTION",
            "WORKSPACE-1",
          ],
          [
            "job_name_2",
            "TEST",
            "WORKSPACE-2",
          ]
        ]
      }
    ]
}

Inside Logic App. I have parsed the json and used a lot of for each loop variations but, every single time, i get stuck in traversing the rows as its a 2d array. Even though, i traverse them, i cannot associate them with columns. Any alternative approaches will be appreciated ?

Input

Expected Output

[ 
   {
      "task": "job_name_1",
      "environment": "PRODUCTION",
      "workspace" : "WORKSPACE-1"
   },
   {
      "task": "job_name_2",
      "environment": "TEST",
      "workspace" : "WORKSPACE-2"
   }
]

Upvotes: 0

Views: 337

Answers (1)

Skin
Skin

Reputation: 11197

I'd be lying if I said this was straight forward but you can load the below definition into your tenant and see a working version.

Note: I made the assumption that you only ever have one table in your top level array given that's what you provided in your question.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_Each_Row": {
                "actions": {
                    "Append_To_Transformed_Array": {
                        "inputs": {
                            "name": "Transformed Array",
                            "value": "@variables('Row Object')"
                        },
                        "runAfter": {
                            "For_Each_Column": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToArrayVariable"
                    },
                    "For_Each_Column": {
                        "actions": {
                            "Add_Property_To_Temp_Row_Object": {
                                "inputs": {
                                    "name": "Temp Row Object",
                                    "value": "@addProperty(variables('Row Object'), variables('Columns')?[variables('Column Index')]['name'], items('For_Each_Column'))"
                                },
                                "runAfter": {},
                                "type": "SetVariable"
                            },
                            "Increment_Column_Index": {
                                "inputs": {
                                    "name": "Column Index",
                                    "value": 1
                                },
                                "runAfter": {
                                    "Set_Row_Object_From_Temp_Row_Object": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "IncrementVariable"
                            },
                            "Set_Row_Object_From_Temp_Row_Object": {
                                "inputs": {
                                    "name": "Row Object",
                                    "value": "@variables('Temp Row Object')"
                                },
                                "runAfter": {
                                    "Add_Property_To_Temp_Row_Object": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "SetVariable"
                            }
                        },
                        "foreach": "@item()",
                        "runAfter": {
                            "Reset_Column_Index": [
                                "Succeeded"
                            ]
                        },
                        "type": "Foreach"
                    },
                    "Reset_Column_Index": {
                        "inputs": {
                            "name": "Column Index",
                            "value": 0
                        },
                        "runAfter": {
                            "Reset_Temp_Row_Object": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    },
                    "Reset_Row_Object": {
                        "inputs": {
                            "name": "Row Object",
                            "value": {}
                        },
                        "runAfter": {},
                        "type": "SetVariable"
                    },
                    "Reset_Temp_Row_Object": {
                        "inputs": {
                            "name": "Temp Row Object",
                            "value": {}
                        },
                        "runAfter": {
                            "Reset_Row_Object": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    }
                },
                "foreach": "@variables('Rows')",
                "runAfter": {
                    "Initialize_Column_Index": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Initialize_Column_Index": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Column Index",
                            "type": "integer"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Temp_Row_Object": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Columns": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Columns",
                            "type": "array",
                            "value": "@variables('Data')?['tables'][0]['columns']"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Data": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Data": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Data",
                            "type": "object",
                            "value": {
                                "tables": [
                                    {
                                        "columns": [
                                            {
                                                "name": "task",
                                                "type": "string"
                                            },
                                            {
                                                "name": "environment",
                                                "type": "string"
                                            },
                                            {
                                                "name": "workspace",
                                                "type": "string"
                                            }
                                        ],
                                        "name": "PrimaryResult",
                                        "rows": [
                                            [
                                                "job_name_1",
                                                "PRODUCTION",
                                                "WORKSPACE-1"
                                            ],
                                            [
                                                "job_name_2",
                                                "TEST",
                                                "WORKSPACE-2"
                                            ]
                                        ]
                                    }
                                ]
                            }
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_Row_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Row Object",
                            "type": "object"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Transformed_Array": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Rows": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Rows",
                            "type": "array",
                            "value": "@variables('Data')?['tables'][0]['rows']"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Columns": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Temp_Row_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Temp Row Object",
                            "type": "object",
                            "value": {}
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Row_Object": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Transformed_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Transformed Array",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Rows": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Result": {
                "inputs": "@variables('Transformed Array')",
                "runAfter": {
                    "For_Each_Row": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "method": "GET",
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

The main challenges I see with having to do this are ...

  1. The lack of ability to self reference. Because of that, I had to use a Row Object in conjunction with a Temp Row Object when adding the property each time.
  2. The amount of nesting is a little hard to follow but it works.
  3. Performance will be a real burden if you have a lot of rows.

One thing to note is the outer For Each action needs to have the concurrency set to 1, if it's not, you'll run into problems.

This is the end result as you were wanting ...

Result

Upvotes: 1

Related Questions