Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

azure - ARM Template trying to merge objects

I'm trying to merge two objects that has an array of objects in it. The two objects has different name/value pairs in the array:

"testobj1": {
  "setting1":"text1",
  "testarray": [
    {
      "name": "testname1",
      "value": "testvalue1"
    },
    {
      "name": "testname1",
      "value": "testvalue1"
    }
  ]
},
"testobj2": {
  "setting2":"text2",
  "testarray": [
    {
      "name": "newtestname",
      "value": "newtestvalue"
    }
  ]
}

I have looked at the documentation for the union function as many suggest, and read other posts like ARM Template concatenate object, but the result is that only the "testarray" from object 2 is kept. I was expecting:

"resultobj": {
  "setting1":"text1",
  "setting2":"text2",  
  "testarray": [
    {
      "name": "testname1",
      "value": "testvalue1"
    },
    {
      "name": "testname1",
      "value": "testvalue1"
    },
    {
      "name": "newtestname",
      "value": "newtestvalue"
    }
  ]
}

But i get:

"resultobj": {
  "setting1":"text1",
  "setting2":"text2",  
  "testarray": [
    {
      "name": "newtestname",
      "value": "newtestvalue"
    }
  ]
}

I know i can use concat to merge arrays, but can′t figure out how to merge these two objects to get the result i expect. Here is the full template for test:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  },
  "variables": {
    "testobj1": {
      "setting1": "text1",
      "testarray": [
        {
          "name": "testname1",
          "value": "testvalue1"
        },
        {
          "name": "testname1",
          "value": "testvalue1"
        }
      ]
    },
    "testobj2": {
      "setting2": "text2",
      "testarray": [
        {
          "name": "newtestname",
          "value": "newtestvalue"
        }
      ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "testUnion": {
      "type": "object",
      "value": "[union(variables('testobj1'),variables('testobj2'))]"
    },
    "testConcat": {
      "type": "array",
      "value": "[concat(variables('testobj1').testarray,variables('testobj2').testarray)]"
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...