如何将数组转换为ARM模板中一个对象的属性? [英] How to convert an array into properties of one object in an ARM template?

查看:79
本文介绍了如何将数组转换为ARM模板中一个对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将数组(例如字符串)转换为一个对象的方法,该对象的属性是根据数组值生成的.

I am looking for a way to convert an array (e.g. of strings) into one object, where the properties are generated from the array values.

用例:我想根据资源名称列表生成一个tags对象,该对象具有指向资源的链接.我需要这样做,以链接App Service资源到Application Insights资源.

Use case: I want to generate a tags object with links to resources, based on a list of resource names. I need to do this, to link App Service resources to an Application Insights resource.

可以使用以下参数提供资源列表:

The list of resources could be supplied using a parameter:

"parameters": {
  "appServices": {
    "type": "array",
    "metadata": {
      "description": "Names of app services to link this application insights resource to via hidden tags"
    }
  }
}

样本输入:

['appName1', 'appName2', 'appName3']

示例输出:

"tags":
{
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName1'))]": "Resource",
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName2'))]": "Resource",
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName3'))]": "Resource"
}

我知道您可以使用 copy 遍历数组,但是会创建一个对象数组,并且不是单个对象(标记必需),例如:

I know you can use copy to loop over arrays but that will create an array of objects and not a single object (which is required for tags), for example:

[
{
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName1'))]": "Resource"
},
{
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName2'))]": "Resource"
},
{
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName3'))]": "Resource"
}
]

可以使用我正在寻找一种动态地实现此目的的方法.

What I am looking for is a way to do this in a dynamic way.

推荐答案

没有直接选项可以将数组转换为对象. 但是,这是实现所需功能的一种方法.这将适用于任何长度的数组.

There is no direct option to convert array to object. But here's a hack to achieve what you need. This will work for array of any length.

步骤:

  1. 将隐藏链接文本附加到服务名称
  2. 将数组转换为字符串
  3. 替换必要的符号并使其成为有效的json字符串.
  4. 使用json()将字符串转换为对象

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "appServices": {
      "type": "array",
      "metadata": {
        "description": "Names of app services to link this application insights resource to via hidden tags"
      },
      "defaultValue": [ "appName1", "appName2", "appName3" ]
    }
  },
  "functions": [],
  "variables": {
    "copy": [
      {
        "name": "as",
        "count": "[length(parameters('appServices'))]",
        "input": "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', parameters('appServices')[copyIndex('as')]))]"
      }
    ],
    "0": "[string(variables('as'))]",
    "1": "[replace(variables('0'), '[', '{')]",
    "2": "[replace(variables('1'), '\",', '\":\"Resource\",')]",
    "3": "[replace(variables('2'), '\"]', '\":\"Resource\"}')]"
  },
  "resources": [],
  "outputs": {
    "op1": {
      "type": "object",
      "value": "[json(variables('3'))]"
    }
  }
}

这篇关于如何将数组转换为ARM模板中一个对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆