I am trying to create an application service to deploy via a CI/CD pipeline, however I am struggling create a For
loop for the connection strings.
Im my parameters file I have the following:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
// Required Parameters
...
// Optional Parameters
"connectionStrings": [
{
"name": "Tracking",
"connectionString": "...",
"type": "SQLServer"
}
{
"name": "Logs",
"connectionString": "...",
"type": "SQLServer"
}
],
...
}
}
I am passing this into the main.bicep
file and the appService.bicep
module file as a param connectionStrings array
.
In the appService.bicep
file I have the following for loop.
resource appServiceWeb 'Microsoft.Web/sites@2022-03-01' = {
name: nameWeb
location: location
tags: tags
kind: kind
identity: {
type: 'SystemAssigned'
}
properties: {
serverFarmId: appServicePlan_id
siteConfig: {
appSettings: [
...
]
connectionStrings: [for connectionString in connectionStrings: {
name: connectionString.name
connectionString: connectionString.connectionString
type: connectionString.type
}]
}
}
}
I am getting the following error:
{
"code": "InvalidRequestContent",
"message": "The request content was invalid and could not be deserialized: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Azure.Deployments.Core.Definitions.DeploymentParameterDefinition' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.rnTo fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.rnPath 'properties.parameters.connectionStrings', line 1, position 280.'."
}
2
Answers
I have managed to fix my issue. While my Json structure was legal, it wasn't the correct structure for bicep files.
I needed to add the
value
part below:Instead of passing
connectionStrings
inparameters.json
file, Include it with an array type inmain.bicep
file. If you pass inparameters.json
it is expecting an object type and that is why for loop is throwing deserialized object error.Usually to avoid this error, Pass
for
loop asAlternatively, I would suggest you add in
main.bicep
file withparam
keyword as detailed above.By referring to the sample bicep template, I made a few changes to your code and was able to deploy it successfully.
main.bicep
:Build & deployment got succeeded:
Deployed a WebApp successfully:
Added connection Strings in
WebApp -> Configuration
: