Search code examples
jsonazureazure-devopsazure-rm-template

How to concatenate a parameter with a string value in ARM template: Unable to parse template language expression


I need to concatenate parameters in the ARM template (JSON file), and I have a problem with "/subscriptions/@{encodeURIComponent([parameters('id')])}/providers". It looks that [parameters('id')] is not recognized as a parameter and it is just passed as a text.

I know that it's possible to use union expression, but in my case I don't need to just merge two separate parameters (according to my understanding, this is what union does). I need to construct a string.

Another option that I have in mind is to do it outside of JSON file, so that I can pass the "/subscriptions/@{encodeURIComponent([parameters('id')])}/providers" as a single parameter. But I find this approach ineffective, because in my ARM template I have 10 similar variables in which I just need to "paste" the value of id.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "id": {
            "type": "string"
        }
    },
    "variables": {},
    "resources": [{
        ...
        "path": "/subscriptions/@{encodeURIComponent([parameters('id')])}/providers",
        ...
    }]
}

Solution

  • In ARM Template you can use concat & uriComponent functions to concatenate parameters:

    "[concat('/subscriptions/', uriComponent(parameters('id')), '/providers')]"
    

    In your example, it would be like:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "id": {
                "type": "string"
            }
        },
        "variables": {},
        "resources": [{
            ...
            "path": "[concat('/subscriptions/', uriComponent(parameters('id')), '/providers')]",
            ...
        }]
    }
    

    Please refer the documentation for concat & uriComponent.