Skip to content

Commit 1c805cd

Browse files
committed
Add tryGet to object function article
1 parent b4fa29d commit 1c805cd

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,16 +748,16 @@ The output from the preceding example with the default values is:
748748

749749
`tryGet(sourceArray, keyOrIndex)`
750750

751-
`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).
751+
`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.
752752

753753
In Bicep, use the [safe-dereference](../bicep/operator-safe-dereference.md#safe-dereference) operator.
754754

755755
### Parameters
756756

757757
| Parameter | Required | Type | Description |
758758
|:--- |:--- |:--- |:--- |
759-
| sourceArray |Yes |array, object |The value to check if it's empty. |
760-
| keyOrIndex |Yes |string, int |The key or index to retrieve from the array or object. A property name for objects or index for arrays.|
759+
| sourceArray |Yes |array, object |An object or array to look into. |
760+
| keyOrIndex |Yes |string, int |A key or index to retrieve from the array or object. A property name for objects or index for arrays.|
761761

762762
### Return value
763763

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Template functions - objects
33
description: Describes the functions to use in an Azure Resource Manager template (ARM template) for working with objects.
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
# Object functions for ARM templates
@@ -540,6 +540,73 @@ The output from the preceding example with the default values is:
540540

541541
**secondOutput** shows the shallow merge doesn't recursively merge these nested objects. Instead, the entire nested object is replaced by the corresponding property from the merging object.
542542

543+
## tryGet
544+
545+
`tryGet(sourceArray, keyOrIndex)`
546+
547+
`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 doesn't exist, `tryGet` returns null instead of throwing an error.
548+
549+
In Bicep, use the [safe-dereference](../bicep/operator-safe-dereference.md#safe-dereference) operator.
550+
551+
### Parameters
552+
553+
| Parameter | Required | Type | Description |
554+
|:--- |:--- |:--- |:--- |
555+
| sourceArray |Yes |array, object |An object or array to look into. |
556+
| keyOrIndex |Yes |string, int |A key or index to retrieve from the array or object. A property name for objects or index for arrays.|
557+
558+
### Return value
559+
560+
Returns the value at the key/index if it exists. Returns null if the key/index is missing or out of bounds.
561+
562+
### Example
563+
564+
The following example checks whether an array, object, and string are empty.
565+
566+
```json
567+
{
568+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
569+
"languageVersion": "2.0",
570+
"contentVersion": "1.0.0.0",
571+
"variables": {
572+
"users": {
573+
"name": "John Doe",
574+
"age": 30
575+
},
576+
"colors": [
577+
"red",
578+
"green"
579+
]
580+
},
581+
"resources": [],
582+
"outputs": {
583+
"region": {
584+
"type": "string",
585+
"nullable": true,
586+
"value": "[tryGet(variables('users'), 'region')]"
587+
},
588+
"name": {
589+
"type": "string",
590+
"nullable": true,
591+
"value": "[tryGet(variables('users'), 'name')]"
592+
},
593+
"firstColor": {
594+
"type": "string",
595+
"nullable": true,
596+
"value": "[tryGet(variables('colors'), 0)]"
597+
}
598+
}
599+
}
600+
```
601+
602+
The output from the preceding example is:
603+
604+
| Name | Type | Value |
605+
| ---- | ---- | ----- |
606+
| region | String | (NULL) |
607+
| name | String | John Doe |
608+
| firstColor | String | Red |
609+
543610
## union
544611

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Template functions
33
description: Describes the functions to use in an Azure Resource Manager template (ARM template) to retrieve values, work with strings and numerics, and retrieve deployment information.
44
ms.topic: reference
55
ms.custom: devx-track-arm-template
6-
ms.date: 07/07/2025
6+
ms.date: 07/10/2025
77
---
88

99
# ARM template functions
@@ -64,6 +64,7 @@ Resource Manager provides several functions for working with arrays.
6464
* [range](template-functions-array.md#range)
6565
* [skip](template-functions-array.md#skip)
6666
* [take](template-functions-array.md#take)
67+
* [tryGet](template-functions-array.md#tryget)
6768
* [tryIndexFromEnd](template-functions-array.md#tryindexfromend)
6869
* [union](template-functions-array.md#union)
6970

@@ -229,6 +230,7 @@ Resource Manager provides several functions for working with objects.
229230
* [null](template-functions-object.md#null)
230231
* [objectKeys](template-functions-object.md#objectkeys)
231232
* [shallowMerge](template-functions-object.md#shallowmerge)
233+
* [tryGet](template-functions-object.md#tryget)
232234
* [union](template-functions-object.md#union)
233235

234236
For Bicep files, use the [object](../bicep/bicep-functions-object.md) functions.

0 commit comments

Comments
 (0)