Skip to content

Commit 28ee8a8

Browse files
committed
add tryGet for array
1 parent a902230 commit 28ee8a8

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

articles/azure-resource-manager/templates/template-functions-array.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Template functions - arrays
33
description: Describes the functions to use in an Azure Resource Manager template (ARM template) for working with arrays.
44
ms.topic: reference
55
ms.custom: devx-track-arm-template
6-
ms.date: 02/12/2025
6+
ms.date: 07/10/2025
77
---
88

99
# Array functions for ARM templates
@@ -693,6 +693,73 @@ The output from the preceding example with the default values is:
693693
| arrayOutput | Array | ["one", "two"] |
694694
| stringOutput | String | on |
695695

696+
## tryGet
697+
698+
`tryGet(sourceArray, keyOrIndex)`
699+
700+
`tryGet` helps you avoid deployment failures when trying to access a non-existent property or index in an object or array. If the specified key or index does not exist, `tryGet` returns null instead of throwing an error. You might need to use the function in conjunction with [nullable types](./data-types.md#nullable-types).
701+
702+
In Bicep, use the [safe-dereference](../bicep/operator-safe-dereference.md#safe-dereference) operator.
703+
704+
### Parameters
705+
706+
| Parameter | Required | Type | Description |
707+
|:--- |:--- |:--- |:--- |
708+
| sourceArray |Yes |array, object |The value to check if it's empty. |
709+
| keyOrIndex |Yes |string, int |The key or index to retrieve from the array or object. A property name for objects or index for arrays.|
710+
711+
### Return value
712+
713+
Returns the value at the key/index if it exists. Returns null if the key/index is missing or out of bounds.
714+
715+
### Example
716+
717+
The following example checks whether an array, object, and string are empty.
718+
719+
```json
720+
{
721+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
722+
"languageVersion": "2.0",
723+
"contentVersion": "1.0.0.0",
724+
"variables": {
725+
"users": {
726+
"name": "John Doe",
727+
"age": 30
728+
},
729+
"colors": [
730+
"red",
731+
"green"
732+
]
733+
},
734+
"resources": [],
735+
"outputs": {
736+
"region": {
737+
"type": "string",
738+
"nullable": true,
739+
"value": "[tryGet(variables('users'), 'region')]"
740+
},
741+
"name": {
742+
"type": "string",
743+
"nullable": true,
744+
"value": "[tryGet(variables('users'), 'name')]"
745+
},
746+
"firstColor": {
747+
"type": "string",
748+
"nullable": true,
749+
"value": "[tryGet(variables('colors'), 0)]"
750+
}
751+
}
752+
}
753+
```
754+
755+
The output from the preceding example is:
756+
757+
| Name | Type | Value |
758+
| ---- | ---- | ----- |
759+
| region | String | (NULL) |
760+
| name | String | John Doe |
761+
| firstColor | String | Red |
762+
696763
## union
697764

698765
`union(arg1, arg2, arg3, ...)`

0 commit comments

Comments
 (0)