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')]"
}
}
]
}
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]"
}
}
}