Arrays and objects in ARM Templates

Please, review the article about ARM templates before : Authoring Azure Resource Manager templates.

Before thinking about Nested templates that is possible to imagine a lightweight scenario based on standard ARM template capabilities. Types of variables and parameters in ARM template are not only scalars and they can represent objects and arrays.

Template parameters can take as an input object, array, and secureObject. Variables are type agnostic. Grace to JSON, syntax for those types using is mainly defined by JavaScript Objects and Arrays.

Use cases:

Access to object field’s value

Object fields are type agnostic.

{
    "parameters": {
        "parameterObject": {
            "type": "object",
            "defaultValue": {
                "objectField1": "stringValue1",
                "objectField2": {
                    "subobjectField1": "subobjectValue1"
                }
            }
        }
    },
    "variables": {
        "objectVariable": {
            "variableObjectField1": "variableObjectValue1",
            "variableObjectField2": {
                "variableSubObjectField1": "variableSubObjecttValue1"
            }
        }
    },
    "resources": [
        {
        /// ... 
        /// to get objectField1 value

        "fieldOfResource": "[parameters('parameterObject').objectField1]",

        /// ...
        /// to get subobjectField1 value

        "fieldOfResource": "[parameters('parameterObject').objectField2.subobjectField1]",
        
        /// ...
        /// to get variableObjectField1 value

        "fieldOfResource": "[variables('objectVariable').variableObjectField1]",

        /// ...
        /// to get variableSubObjectField1 value

        "fieldOfResource": "[variables('objectVariable').variableObjectField2.variableObjectField2]",
        
        /// ...
        /// if array or object represents part of JSON template schema : 
        
        "fieldOfResource": "[parameters('parameterObject')]"
        
        /// object or array replaces "[parameters('parameterObject')]"
        /// kind of nested resource
        
        /// ...
        }
    ]
}

Access to array’s elements

Array elements are type agnostic.

{
    "parameters": {
        "parameterObject": {
            "type": "array",
            "defaultValue": [
                "value1",
                "value2",
                "value3",
                "value4"
            ]
        }
    },
    "variables": {
        "indexN": 1
    },
    "resources": [
        {
        /// ... 
        /// to get objectField1 value

        "fieldOfResource": "[parameters('parameterObject')[0]]",
        
        /// "value1" replaces "[parameters('parameterObject')[0]]" 
        
        /// ... 
        
        "fieldOfResource": "[parameters('parameterObject')[variables('indexN')]]",
        
        /// "value2" replaces "[parameters('parameterObject')[variables('indexN')]]"
        
        /// ...
        /// Multiple istances of resource 
        /// https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple
        
        "copy": {
            "name": "loopName",
            "count": "[length(parameters('parameterObject'))]"
        },
        
        /// [length(parameters('parameterObject'))] == 4 
        /// 4 instances will be created 
        
        "fieldOfResource": "[parameters('parameterObject').objectField1.subobjectField1]"
        
        /// ...
        }
    ]
}

Associative array

{
    "parameters": {
        "parameterObject": {
            "type": "object",
            "defaultValue": {
                "objectField1": "stringValue1",
                "objectField2": {
                    "subobjectField1": "subobjectValue1"
                }
            }
        }
    },
    "variables": {
        "fieldToGetValue": "objectField1",
        "subfieldToGetValue": "subobjectField1",
        "variablewithsuffix-0": {
            "variableObjectValue": "value1"
        },
        "variablewithsuffix-1": {
            "variableObjectValue": "value2"
        },
        "variablewithsuffix-2": {
            "variableObjectValue": "value3"
        },
        "variablewithsuffix-3": {
            "variableObjectValue": "value4"
        },
        "variablewithsuffix-count": 4
    },
    "resources": [
        {
        /// ...
        
        "fieldOfResource": "[parameters('parameterObject')[variables('fieldToGetValue')]]",
        
        /// ...
        
        "fieldOfResource": "[parameters('parameterObject').objectField1[variables('subfieldToGetValue')]]",
        
        /// ...
        
        "fieldOfResource": "[parameters('parameterObject')[variables('fieldToGetValue')][variables('subfieldToGetValue')]]",
        
        /// ...
        /// Multiple istances of resource 
        /// https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple
        
        "copy": {
            "name": "loopName",
            "count": "[variables('variablewithsuffix-count')]"
        },
                
        "fieldOfResource": "[variables(concat('variablewithsuffix-', copyIndex())).variableObjectValue]"
        
        /// ...
        }
    ]
}

Arrays and objects combination

Array as a value of an object field

{
    "parameters": {
        "parameterObject": {
            "type": "object",
            "defaultValue": {
                "objectField1": "stringValue1",
                "objectField2": {
                    "subobjectField1": "subobjectValue1"
                },
                "objectArrayField1": [
                    "value1",
                    "value2",
                    "value3",
                    "value4"
                ]
            }
        }
    },
    "resources": [
        {
        /// ... 
        /// to get first element of objectArrayField1

        "fieldOfResource": "[parameters('parameterObject').objectArrayField1[0]]"

        /// ... 
        }
    ]
}

Array of objects

{
    "parameters": {
        "parameterObject": {
            "type": "array",
            "defaultValue": [
                {
                    "commonnamedfiled": "value1",
                    "field1object1": "field1object1value"
                },
                {
                    "commonnamedfiled": "value2",
                    "field1object2": "field1object2value"
                },
                {
                    "commonnamedfiled": "value3",
                    "field1object3": "field1object3value"
                }
            ]
        }
    },
    "resources": [
        {
        /// ... 
        /// to get field1object3

        "fieldOfResource": "[parameters('parameterObject')[2].field1object3]",
        
        /// "field1object3value" replaces "[parameters('parameterObject')[2].field1object3]"
        
        /// ...
        /// Multiple istances of resource 
        /// https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple
        
        "copy": {
            "name": "loopName",
            "count": "[length(parameters('parameterObject'))]"
        },
        
        /// [length(parameters('parameterObject'))] == 3 
        /// 3 instances will be created
        
        "fieldOfResource": "[parameters('parameterObject')[copyIndex()].commonnamedfiled]"
        
        /// ...
        }
    ]
}