Search code examples
arraylistazure-logic-apps

Azure Logic App - Add to existing ArrayList in a Object


I am trying to create a LogicApp which can add to an existing object. I initialized two variables in the below details.

Object1
{
  "Email": "[email protected]",
  "FirstName": "test",
  "LastName": "tester",
  "Memberships": [
    {
      "id": "1"
    },
    {
      "id": "2"
    }
  ]
}

Object2
{
  "Memberships": [
    {
      "id": "3"
    }
  ]
}

FinalResult
{
  "Email": "[email protected]",
  "FirstName": "test",
  "LastName": "tester",
  "Memberships": [
    {
      "id": "1"
    },
    {
      "id": "2"
    },
    {
      "id": "3"
    }
  ]
}

Using a Compose Component I tried union(variable(object1),variable(object2)) and this did not add Membership ID 3.

Any help is appreciated.


Solution

  • I have reproduced in my environment and got expected results as below:

    Design:

    enter image description here

    clearly:

    union(variables('var1')['Memberships'],variables('var2')['Memberships'])
    

    enter image description here

    Output:

    enter image description here

    Codeview:

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Compose": {
                    "inputs": "@union(variables('var1')['Memberships'],variables('var2')['Memberships'])",
                    "runAfter": {
                        "Parse_JSON_2": [
                            "Succeeded"
                        ]
                    },
                    "type": "Compose"
                },
                "Initialize_variable": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "var1",
                                "type": "object",
                                "value": {
                                    "Email": "[email protected]",
                                    "FirstName": "test",
                                    "LastName": "tester",
                                    "Memberships": [
                                        {
                                            "id": "1"
                                        },
                                        {
                                            "id": "2"
                                        }
                                    ]
                                }
                            }
                        ]
                    },
                    "runAfter": {},
                    "type": "InitializeVariable"
                },
                "Initialize_variable_2": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "var2",
                                "type": "object",
                                "value": {
                                    "Memberships": [
                                        {
                                            "id": "3"
                                        }
                                    ]
                                }
                            }
                        ]
                    },
                    "runAfter": {
                        "Parse_JSON": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Initialize_variable_3": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "var3",
                                "type": "object",
                                "value": {
                                    "Email": "@body('Parse_JSON')?['Email']",
                                    "FirstName": "@body('Parse_JSON')?['FirstName']",
                                    "LatsName": "@body('Parse_JSON')?['LastName']",
                                    "Memberships": "@outputs('Compose')"
                                }
                            }
                        ]
                    },
                    "runAfter": {
                        "Compose": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Parse_JSON": {
                    "inputs": {
                        "content": "@variables('var1')",
                        "schema": {
                            "properties": {
                                "Email": {
                                    "type": "string"
                                },
                                "FirstName": {
                                    "type": "string"
                                },
                                "LastName": {
                                    "type": "string"
                                },
                                "Memberships": {
                                    "items": {
                                        "properties": {
                                            "id": {
                                                "type": "string"
                                            }
                                        },
                                        "required": [
                                            "id"
                                        ],
                                        "type": "object"
                                    },
                                    "type": "array"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "runAfter": {
                        "Initialize_variable": [
                            "Succeeded"
                        ]
                    },
                    "type": "ParseJson"
                },
                "Parse_JSON_2": {
                    "inputs": {
                        "content": "@variables('var2')",
                        "schema": {
                            "properties": {
                                "Memberships": {
                                    "items": {
                                        "properties": {
                                            "id": {
                                                "type": "string"
                                            }
                                        },
                                        "required": [
                                            "id"
                                        ],
                                        "type": "object"
                                    },
                                    "type": "array"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "runAfter": {
                        "Initialize_variable_2": [
                            "Succeeded"
                        ]
                    },
                    "type": "ParseJson"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {},
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {}
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {}
    }