Skip to content

Commit b5374c3

Browse files
authored
Merge pull request #274900 from mumian/0509-function-objectkeys
ARM/Bicep function objectKeys
2 parents 9ab175f + 714df94 commit b5374c3

File tree

4 files changed

+107
-17
lines changed

4 files changed

+107
-17
lines changed

articles/azure-resource-manager/bicep/bicep-functions-object.md

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bicep functions - objects
33
description: Describes the functions to use in a Bicep file for working with objects.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 01/11/2024
6+
ms.date: 05/21/2024
77
---
88

99
# Object functions for Bicep
@@ -31,7 +31,7 @@ Namespace: [sys](bicep-functions.md#namespaces-for-functions).
3131

3232
### Example
3333

34-
The following example shows how to use contains with different types:
34+
The following example shows how to use `contains` with different types:
3535

3636
```bicep
3737
param stringToTest string = 'OneTwoThree'
@@ -119,15 +119,15 @@ Namespace: [sys](bicep-functions.md#namespaces-for-functions).
119119
|:--- |:--- |:--- |:--- |
120120
| arg1 |Yes |array or object |The first value to use for finding common elements. |
121121
| arg2 |Yes |array or object |The second value to use for finding common elements. |
122-
| additional arguments |No |array or object |Additional values to use for finding common elements. |
122+
| additional arguments |No |array or object |More values to use for finding common elements. |
123123

124124
### Return value
125125

126126
An array or object with the common elements.
127127

128128
### Example
129129

130-
The following example shows how to use intersection with arrays and objects:
130+
The following example shows how to use `intersection` with arrays and objects:
131131

132132
```bicep
133133
param firstObject object = {
@@ -301,7 +301,7 @@ If you need to include a parameter value or variable in the JSON object, use the
301301

302302
### Example
303303

304-
The following example shows how to use the json function. Notice that you can pass in **null** for an empty object.
304+
The following example shows how to use the `json` function. Notice that you can pass in **null** for an empty object.
305305

306306
```bicep
307307
param jsonEmptyObject string = 'null'
@@ -353,7 +353,7 @@ An int.
353353

354354
### Example
355355

356-
The following example shows how to use length with an array and string:
356+
The following example shows how to use `length` with an array and string:
357357

358358
```bicep
359359
param arrayToTest array = [
@@ -385,11 +385,49 @@ The output from the preceding example with the default values is:
385385
| stringLength | Int | 13 |
386386
| objectLength | Int | 4 |
387387

388+
## objectKeys
389+
390+
`objectKeys(object)`
391+
392+
Returns the keys from an object, where an object is a collection of key-value pairs.
393+
394+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
395+
396+
### Parameters
397+
398+
| Parameter | Required | Type | Description |
399+
|:--- |:--- |:--- |:--- |
400+
| object |Yes |object |The object, which is a collection of key-value pairs. |
401+
402+
### Return value
403+
404+
An array.
405+
406+
### Example
407+
408+
The following example shows how to use `objectKeys` with an object:
409+
410+
```bicep
411+
var obj = { a: 1, b: 2 }
412+
413+
output keyArray array = objectKeys(obj)
414+
```
415+
416+
The output from the preceding example is:
417+
418+
| Name | Type | Value |
419+
| ---- | ---- | ----- |
420+
| keyArray | Array | [ "a", "b" ] |
421+
422+
**keyArray** returns a list of keys of the input object.
423+
424+
[!INCLUDE [JSON object ordering](../../../includes/resource-manager-object-ordering-bicep.md)]
425+
388426
## shallowMerge
389427

390428
`shallowMerge(inputArray)`
391429

392-
Combines an array of objects, where only the top-level objects are merged. This means that if the objects being merged contain nested objects, those nested object aren't deeply merged; instead, they're replaced entirely by the corresponding property from the merging object.
430+
Combines an array of objects, where only the top-level objects are merged. This means that if the objects being merged contain nested objects, those nested object aren't deeply merged. Instead, they're replaced entirely by the corresponding property from the merging object.
393431

394432
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
395433

@@ -422,7 +460,7 @@ The output from the preceding example with the default values is:
422460
| firstOutput | object | {"one":"a","two":"c"}|
423461
| secondOutput | object | {"one":"a","nested":{"b":2},"two":"b"} |
424462

425-
**firstOutput** shows the properties from the merging objects are combined into a new object. If there are conflicting properties (i.e., properties with the same name), the property from the last object being merged usually takes precedence.
463+
**firstOutput** shows the properties from the merging objects are combined into a new object. If there are conflicting properties (that is, properties with the same name), the property from the last object being merged usually takes precedence.
426464

427465
**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.
428466

@@ -440,7 +478,7 @@ Namespace: [sys](bicep-functions.md#namespaces-for-functions).
440478
|:--- |:--- |:--- |:--- |
441479
| arg1 |Yes |array or object |The first value to use for joining elements. |
442480
| arg2 |Yes |array or object |The second value to use for joining elements. |
443-
| additional arguments |No |array or object |Additional values to use for joining elements. |
481+
| additional arguments |No |array or object |More values to use for joining elements. |
444482

445483
### Return value
446484

@@ -450,15 +488,15 @@ An array or object.
450488

451489
The union function uses the sequence of the parameters to determine the order and values of the result.
452490

453-
For arrays, the function iterates through each element in the first parameter and adds it to the result if it isn't already present. Then, it repeats the process for the second parameter and any additional parameters. If a value is already present, it's earlier placement in the array is preserved.
491+
For arrays, the function iterates through each element in the first parameter and adds it to the result if it isn't already present. Then, it repeats the process for the second parameter and any other parameters. If a value is already present, it's earlier placement in the array is preserved.
454492

455493
For objects, property names and values from the first parameter are added to the result. For later parameters, any new names are added to the result. If a later parameter has a property with the same name, that value overwrites the existing value. The order of the properties isn't guaranteed.
456494

457495
The union function merges not only the top-level elements but also recursively merges any nested objects within them. Nested array values aren't merged. See the second example in the following section.
458496

459497
### Example
460498

461-
The following example shows how to use union with arrays and objects:
499+
The following example shows how to use `union` with arrays and objects:
462500

463501
```bicep
464502
param firstObject object = {

articles/azure-resource-manager/bicep/bicep-functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bicep functions
33
description: Describes the functions to use in a Bicep file to retrieve values, work with strings and numerics, and retrieve deployment information.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 05/10/2024
6+
ms.date: 05/21/2024
77
---
88

99
# Bicep functions
@@ -121,6 +121,7 @@ The following functions are available for working with objects. All of these fun
121121
* [items](./bicep-functions-object.md#items)
122122
* [json](./bicep-functions-object.md#json)
123123
* [length](./bicep-functions-object.md#length)
124+
* [objectKeys](./bicep-functions-object.md#objectkeys)
124125
* [shallowMerge](./bicep-functions-object.md#shallowmerge)
125126
* [union](./bicep-functions-object.md#union)
126127

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

Lines changed: 55 additions & 5 deletions
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: conceptual
55
ms.custom: devx-track-arm-template
6-
ms.date: 01/11/2024
6+
ms.date: 05/21/2024
77
---
88

99
# Object functions for ARM templates
@@ -44,7 +44,7 @@ In Bicep, use the [contains](../bicep/bicep-functions-object.md#contains) functi
4444

4545
### Example
4646

47-
The following example shows how to use contains with different types:
47+
The following example shows how to use `contains` with different types:
4848

4949
:::code language="json" source="~/resourcemanager-templates/azure-resource-manager/functions/array/contains.json":::
5050

@@ -154,7 +154,7 @@ An array or object with the common elements.
154154

155155
### Example
156156

157-
The following example shows how to use intersection with arrays and objects.
157+
The following example shows how to use `intersection` with arrays and objects.
158158

159159
:::code language="json" source="~/resourcemanager-templates/azure-resource-manager/functions/array/intersection.json":::
160160

@@ -369,7 +369,7 @@ An int.
369369

370370
### Example
371371

372-
The following example shows how to use length with an array and string:
372+
The following example shows how to use `length` with an array and string:
373373

374374
:::code language="json" source="~/resourcemanager-templates/azure-resource-manager/functions/array/length.json":::
375375

@@ -409,6 +409,56 @@ The output from the preceding example is:
409409
| ---- | ---- | ----- |
410410
| emptyOutput | Bool | True |
411411

412+
## objectKeys
413+
414+
`objectKeys(object)`
415+
416+
Returns the keys from an object, where an object is a collection of key-value pairs.
417+
418+
In Bicep, use the [objectKeys](../templates/template-functions-object.md#objectkeys) function.
419+
420+
### Parameters
421+
422+
| Parameter | Required | Type | Description |
423+
|:--- |:--- |:--- |:--- |
424+
| object |Yes |object |The object, which is a collection of key-value pairs. |
425+
426+
### Return value
427+
428+
An array.
429+
430+
### Example
431+
432+
The following example shows how to use `objectKeys` with an object:
433+
434+
```json
435+
{
436+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
437+
"contentVersion": "1.0.0.0",
438+
"variables": {
439+
"obj": {
440+
"a": 1,
441+
"b": 2
442+
}
443+
},
444+
"resources": [],
445+
"outputs": {
446+
"keyArray": {
447+
"type": "array",
448+
"value": "[objectKeys(variables('obj'))]"
449+
}
450+
}
451+
}
452+
```
453+
454+
The output from the preceding example is:
455+
456+
| Name | Type | Value |
457+
| ---- | ---- | ----- |
458+
| keyArray | Array | [ "a", "b" ] |
459+
460+
[!INCLUDE [JSON object ordering](../../../includes/resource-manager-object-ordering-arm-template.md)]
461+
412462
## shallowMerge
413463

414464
`shallowMerge(inputArray)`
@@ -522,7 +572,7 @@ The union function merges not only the top-level elements but also recursively m
522572

523573
### Example
524574

525-
The following example shows how to use union with arrays and objects:
575+
The following example shows how to use `union` with arrays and objects:
526576

527577
:::code language="json" source="~/resourcemanager-templates/azure-resource-manager/functions/array/union.json":::
528578

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Resource Manager provides several functions for working with objects.
220220
* [items](template-functions-object.md#items)
221221
* [json](template-functions-object.md#json)
222222
* [length](template-functions-object.md#length)
223+
* [objectKeys](template-functions-object.md#objectkeys)
223224
* [null](template-functions-object.md#null)
224225
* [shallowMerge](template-functions-object.md#shallowmerge)
225226
* [union](template-functions-object.md#union)

0 commit comments

Comments
 (0)