在 ARM 模板的 if 语句中格式化 JSON 包含单引号和双引号
Formatting JSON containing single and double quotes within if statement in ARM template
我正在尝试在条件中指定 Microsoft 的 storageProfile 属性。Compute/virtualMachine 资源方式。
根据某个参数是否设置,应该使用某个JSON。
根据以下文档页面中的示例,这应该是可能的:
https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-logical#if
一个说明问题的简化示例:
"name": "string",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2019-03-01",
"location": "string",
"tags": {},
"properties": {
"hardwareProfile": {
"vmSize": "string"
},
"storageProfile": "[if(greater(parameters('myParameter', 0)), json('{\\"dataDisks\\": variables(\'myFirstVariable\')}'), json('{\\"dataDisks\\": variables(\'mySecondVariable\')}'))]"
我正在努力的部分是 storageProfile 属性.
在调试模式下提交包含上述内容的 ARM 模板时出现错误。
cli.azure.cli.core.util : Failed to parse
test.json with exception:
Invalid \escape: line 275 column 110 (char 10373) Failed to parse
test.json with exception:
Invalid \escape: line 275 column 110 (char 10373)
第 275 行的字符 110 是冒号“:”。
如果两个 JSON 对象同时包含双引号和单引号,正确的请求应该是什么样的?
引号前应有单反斜杠(\)。尝试以下:
{
"name": "string",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2019-03-01",
"location": "string",
"tags": {
},
"properties": {
"hardwareProfile": {
"vmSize": "string"
},
"storageProfile": "[if(greater(parameters('myParameter', 0)), json('{\"dataDisks\": variables(\'myFirstVariable\')}'), json('{\"dataDisks\": variables(\'mySecondVariable\')}'))]"
}
}
你错了,错的不是if语句,而是更大的函数。应该是这样的:
greater(parameters('myParameter'), 0)
您可以获得有关 greater
here 的更多详细信息。 json
函数似乎也是错误的格式,示例如下:
json('{\"a\": \"b\"}')
有关 json
函数的更多详细信息 here。
我在您发布的问题中找到的所有错误都在上面。如果还有其他错误,您需要提供更多详细信息。希望对你有帮助。
我正在尝试在条件中指定 Microsoft 的 storageProfile 属性。Compute/virtualMachine 资源方式。
根据某个参数是否设置,应该使用某个JSON。 根据以下文档页面中的示例,这应该是可能的: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-logical#if
一个说明问题的简化示例:
"name": "string",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2019-03-01",
"location": "string",
"tags": {},
"properties": {
"hardwareProfile": {
"vmSize": "string"
},
"storageProfile": "[if(greater(parameters('myParameter', 0)), json('{\\"dataDisks\\": variables(\'myFirstVariable\')}'), json('{\\"dataDisks\\": variables(\'mySecondVariable\')}'))]"
我正在努力的部分是 storageProfile 属性.
在调试模式下提交包含上述内容的 ARM 模板时出现错误。
cli.azure.cli.core.util : Failed to parse test.json with exception: Invalid \escape: line 275 column 110 (char 10373) Failed to parse test.json with exception: Invalid \escape: line 275 column 110 (char 10373)
第 275 行的字符 110 是冒号“:”。
如果两个 JSON 对象同时包含双引号和单引号,正确的请求应该是什么样的?
引号前应有单反斜杠(\)。尝试以下:
{
"name": "string",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2019-03-01",
"location": "string",
"tags": {
},
"properties": {
"hardwareProfile": {
"vmSize": "string"
},
"storageProfile": "[if(greater(parameters('myParameter', 0)), json('{\"dataDisks\": variables(\'myFirstVariable\')}'), json('{\"dataDisks\": variables(\'mySecondVariable\')}'))]"
}
}
你错了,错的不是if语句,而是更大的函数。应该是这样的:
greater(parameters('myParameter'), 0)
您可以获得有关 greater
here 的更多详细信息。 json
函数似乎也是错误的格式,示例如下:
json('{\"a\": \"b\"}')
有关 json
函数的更多详细信息 here。
我在您发布的问题中找到的所有错误都在上面。如果还有其他错误,您需要提供更多详细信息。希望对你有帮助。