0

Using Azure Logic Apps I would like I would like to merge the Vehicles of Sample Item 1 and Sample Item 2, how can I do this? LogicApp Code can be found here: https://pastebin.com/ntt9biYU

The Sample Item 1 is the main, Sample Item 2 Vehicles need to be appended to Sample Item 1. I've tried the following without luck:

setProperty(json(string(variables('sampleItem1')))['Vehicles'], json(string(variables('sampleItem2')))['Vehicles'])

Main (Sample Item 1)

{
  "Name": "SomeName ",
  "Vehicles": [
    {
      "Brand": "Volkswagen",
      "Color": "Blue"
    },
    {
      "Brand": "BMW",
      "Color": "Black"
    }
  ]
}

Sample item 2 (to merge)

{
  "Name": "SomeName",
  "Vehicles": [
    {
      "Brand": "Mercedes",
      "Color": "White"
    }
  ]
}

Expected result:

{
  "Name": "SomeName ",
  "Vehicles": [
    {
      "Brand": "Volkswagen",
      "Color": "Blue"
    },
    {
      "Brand": "BMW",
      "Color": "Black"
    },
    {
      "Brand": "Mercedes",
      "Color": "White"
    }
  ]
}
Skin
  • 9,085
  • 2
  • 13
  • 29
Jay
  • 189
  • 4
  • 12

1 Answers1

0

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

Uisng setproperty:

setProperty(variables('var1'), 'Vehicles', union(variables('var1')['Vehicles'], variables('var2')['Vehicles']))

Design:

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": "@setProperty(variables('var1'), 'Vehicles', union(variables('var1')['Vehicles'], variables('var2')['Vehicles']))\r\n",
                "runAfter": {
                    "Initialize_variable_2": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "var1",
                            "type": "object",
                            "value": {
                                "Name": "SomeName ",
                                "Vehicles": [
                                    {
                                        "Brand": "Volkswagen",
                                        "Color": "Blue"
                                    },
                                    {
                                        "Brand": "BMW",
                                        "Color": "Black"
                                    }
                                ]
                            }
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "var2",
                            "type": "object",
                            "value": {
                                "Name ": "SomeName ",
                                "Vehicles": [
                                    {
                                        "Brand": "Mercedes",
                                        "Color": "White"
                                    }
                                ]
                            }
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

Uisng union:

Design:

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

enter image description here

Output:

enter image description here

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')['Vehicles'], variables('var2')['Vehicles'])\r\n",
                "runAfter": {
                    "Initialize_variable_2": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Compose_2": {
                "inputs": {
                    "Name ": "SomeName ",
                    "Vehicles": "@outputs('Compose')"
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "var1",
                            "type": "object",
                            "value": {
                                "Name": "SomeName ",
                                "Vehicles": [
                                    {
                                        "Brand": "Volkswagen",
                                        "Color": "Blue"
                                    },
                                    {
                                        "Brand": "BMW",
                                        "Color": "Black"
                                    }
                                ]
                            }
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_variable_2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "var2",
                            "type": "object",
                            "value": {
                                "Name ": "SomeName ",
                                "Vehicles": [
                                    {
                                        "Brand": "Mercedes",
                                        "Color": "White"
                                    }
                                ]
                            }
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

Try to replicate the same and you will get the output as i have got.

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7