json - 获取 ARM 模板中的对象属性名称数组
问题描述
在 ARM 模板中,有没有办法获取包含 JSON 对象属性名称的数组?我在文档中看不到任何明显的东西。我看到的最接近的是length(object)
获取对象的属性计数,但我认为我什至不能使用复制循环来获取属性名称。
我要实现的特定场景是将appsettings
具有附加插槽粘性设置的 Web 部署到暂存插槽:
{
"$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')]"
}
}
]
}
解决方案
没有一种直接的方法可以做到这一点,因为没有函数可以返回对象的属性。我能够通过将对象转换为字符串然后解析它以找到属性名称来完成它。只要您的属性值中没有逗号,它就应该起作用。如果需要,您可能还可以添加一些检查来处理这种情况。
{
"$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]"
}
}
}
推荐阅读
- flutter - Linkify TextField 并在 Flutter 中显示链接预览
- kotlin - Kotlin 使用两种 Any 类型,可以是 Int 或 Float 进行加法/减法/乘法/减法
- r - 设置子图颜色 plotly R
- excel - 如果循环内的语句不断崩溃
- powershell - Powershell 哈希:添加/更新值是否容易?
- python - 递归中的全局计数器
- azure-pipelines-yaml - Azure yaml 管道:从任务 SqlAzureDacpacDeployment 获取部署 SQL 脚本
- azure-iot-edge - Azure IoT Edge 模块无法进行身份验证
- python - 将文本解析为列
- javascript - 在Angular中将Iframe数据捕获到图像(png)中