Home > Software engineering >  Use parameter to index into JSON object in Bicep
Use parameter to index into JSON object in Bicep

Time:11-14

I am creating an Azure deployment that will be used across the various regions. I am trying to keep the code agnostic to region and provide region specific info via a JSON file.

The JSON file will have the format of

{
    "region1":
        {
           "key" : "value"
        },
    "region2":
        {
           "key" : "value"
        }
}

I would like to import the JSON at deployment time and use the values for a specific region by taking an input of the target region to be deployed and storing it in the parameter called region. I then want to use the region parameter to index into the JSON object like the example below:

param _regions object = json(loadTextContent('<json_file_name>'))
param region string

var regionProperty = _regions.${region}.key

Using the syntax above for indexing into the JSON does not work, does anyone have ideas on how I can make this work?

CodePudding user response:

Looking at the documentation:

You can use the [] syntax to access a property.

var regionProperty = _regions[region].key
  • Related