1

In an ARM template is there a way to get an array containing a JSON object's property names? I don't see anything obvious in the documentation. The closest thing I see is length(object) to get the object's property count but I don't think I could even use a copy loop to get the property names.

The specific scenario I want to implement is deploying web appsettings with additional slot-sticky settings to a staging slot:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "webSiteName": {
      "type": "string"
    },
    "globalAppSettings": {
      "type": "object"
    },
    "slotName": {
      "type": "string"
    },
    "slotAppSettings": {
      "type": "object"
    },
    "slotStickySettings": {
      "type": "array",
      // but getPropertyNames(object) is not a real function :(
      "defaultValue": "[getPropertyNames(parameters('slotAppSettings'))]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites/config",
      "name": "[concat(parameters('webSiteName'), '/appsettings')]",
      "apiVersion": "2018-02-01",
      "properties": "[parameters('globalAppSettings')]"
    },
    {
      "type": "Microsoft.Web/sites/slots/config",
      "name": "[concat(parameters('webSiteName'), '/', parameters('slotName'), '/appsettings')]",
      "apiVersion": "2018-02-01",
      "properties": "[union(parameters('globalAppSettings'), parameters('slotAppSettings'))]"
    },
    {
      "type": "Microsoft.Web/sites/config",
      "name": "[concat(parameters('webSiteName'), '/slotconfignames')]",
      "apiVersion": "2015-08-01",
      "properties": {
        "appSettingNames": "[parameters('slotStickySettings')]"
      }
    }
  ]
}
Serguei
  • 2,910
  • 3
  • 24
  • 34
  • i'm not sure what you are after exactly? what information you want to gather and then iterate? – 4c74356b41 Jan 07 '20 at 20:31
  • @4c74356b41, for example if `"slotAppSettings": { "s1": 1, "s2": 2, "s3": 3 }` then I want to compute `"slotStickySettings": ["s1", "s2", "s3"]` (that's the `getPropertyNames()` placeholder function in the template). – Serguei Jan 08 '20 at 17:52

1 Answers1

2

There is not a straightforward way of doing this as there is no function to return the properties of an object. I was able to accomplish it by converting the the object to a string and then parsing it to find the property names. It should work as long as you don't have commas in your property values. You could probably add some checks to handle that case as well if needed.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "testSettings": {
            "type": "object",
            "defaultValue": {
                "a": "demo value 1",
                "b": "demo value 2"
            }
        }
    },
    "variables": {
        "delimiters": [","],
        "settingArray": "[split(replace(replace(replace(string(parameters('testSettings')), '{', ''), '}', ''), '\"', ''), variables('delimiters'))]",
        "propNameArray": {
            "copy": [
                {
                    "name": "copyPropertyNames",
                    "count": "[length(variables('settingArray'))]",
                    "input": "[substring(variables('settingArray')[copyIndex('copyPropertyNames')], 0, indexOf(variables('settingArray')[copyIndex('copyPropertyNames')], ':'))]"
                }
            ]
        }
    },
    "resources": [],
    "outputs": {
        "paramOutput": {
            "type": "array",
            "value": "[variables('propNameArray').copyPropertyNames]"
        }
    }
}
PerfectlyPanda
  • 3,271
  • 1
  • 6
  • 17