|
| 1 | +--- |
| 2 | +description: Reference for the 'createObject' DSC configuration document function |
| 3 | +ms.date: 07/28/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: createObject |
| 6 | +--- |
| 7 | + |
| 8 | +## Synopsis |
| 9 | + |
| 10 | +Creates a JSON object from key-value pairs. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```Syntax |
| 15 | +createObject(<key1>, <value1>, <key2>, <value2>, ...) |
| 16 | +``` |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +The `createObject()` function creates a JSON object from the provided key-value pairs. |
| 21 | +Arguments must be provided in pairs where the first argument of each pair is a string key, |
| 22 | +and the second argument is the value of any type. |
| 23 | + |
| 24 | +If no arguments are provided, the function returns an empty object. The number of arguments |
| 25 | +must be even, as they represent key-value pairs. |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +### Example 1 - Basic object creation |
| 30 | + |
| 31 | +The following example shows how to create simple objects with string and numeric values. |
| 32 | + |
| 33 | +```yaml |
| 34 | +# createObject.example.1.dsc.config.yaml |
| 35 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 36 | +resources: |
| 37 | +- name: Basic object creation |
| 38 | + type: Microsoft.DSC.Debug/Echo |
| 39 | + properties: |
| 40 | + output: |
| 41 | + simpleObject: "[createObject('name', 'test')]" |
| 42 | + multipleProps: "[createObject('key1', 'value1', 'key2', 42)]" |
| 43 | + emptyObject: "[createObject()]" |
| 44 | +``` |
| 45 | +
|
| 46 | +```bash |
| 47 | +dsc config get --file createObject.example.1.dsc.config.yaml |
| 48 | +``` |
| 49 | + |
| 50 | +```yaml |
| 51 | +results: |
| 52 | +- name: Basic object creation |
| 53 | + type: Microsoft.DSC.Debug/Echo |
| 54 | + result: |
| 55 | + actualState: |
| 56 | + output: |
| 57 | + simpleObject: |
| 58 | + name: test |
| 59 | + multipleProps: |
| 60 | + key1: value1 |
| 61 | + key2: 42 |
| 62 | + emptyObject: {} |
| 63 | +messages: [] |
| 64 | +hadErrors: false |
| 65 | +``` |
| 66 | +
|
| 67 | +### Example 2 - Mixed data types |
| 68 | +
|
| 69 | +The following example shows how to create objects with different value types. |
| 70 | +
|
| 71 | +```yaml |
| 72 | +# createObject.example.2.dsc.config.yaml |
| 73 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 74 | +resources: |
| 75 | +- name: Mixed data types |
| 76 | + type: Microsoft.DSC.Debug/Echo |
| 77 | + properties: |
| 78 | + output: "[createObject('string', 'hello', 'number', 123, 'boolean', true, 'nullValue', null())]" |
| 79 | +``` |
| 80 | +
|
| 81 | +```bash |
| 82 | +dsc config get --file createObject.example.2.dsc.config.yaml |
| 83 | +``` |
| 84 | + |
| 85 | +```yaml |
| 86 | +results: |
| 87 | +- name: Mixed data types |
| 88 | + type: Microsoft.DSC.Debug/Echo |
| 89 | + result: |
| 90 | + actualState: |
| 91 | + output: |
| 92 | + string: hello |
| 93 | + number: 123 |
| 94 | + boolean: true |
| 95 | + nullValue: null |
| 96 | +messages: [] |
| 97 | +hadErrors: false |
| 98 | +``` |
| 99 | +
|
| 100 | +### Example 3 - Nested objects and arrays |
| 101 | +
|
| 102 | +The following example shows how to create objects containing other objects and arrays. |
| 103 | +
|
| 104 | +```yaml |
| 105 | +# createObject.example.3.dsc.config.yaml |
| 106 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 107 | +resources: |
| 108 | +- name: Nested structures |
| 109 | + type: Microsoft.DSC.Debug/Echo |
| 110 | + properties: |
| 111 | + output: |
| 112 | + nestedObject: "[createObject('config', createObject('timeout', 30, 'enabled', true))]" |
| 113 | + objectWithArray: "[createObject('items', createArray('foo', 'bar', 'baz'))]" |
| 114 | +``` |
| 115 | +
|
| 116 | +```bash |
| 117 | +dsc config get --file createObject.example.3.dsc.config.yaml |
| 118 | +``` |
| 119 | + |
| 120 | +```yaml |
| 121 | +results: |
| 122 | +- name: Nested structures |
| 123 | + type: Microsoft.DSC.Debug/Echo |
| 124 | + result: |
| 125 | + actualState: |
| 126 | + output: |
| 127 | + nestedObject: |
| 128 | + config: |
| 129 | + timeout: 30 |
| 130 | + enabled: true |
| 131 | + objectWithArray: |
| 132 | + items: |
| 133 | + - foo |
| 134 | + - bar |
| 135 | + - baz |
| 136 | +messages: [] |
| 137 | +hadErrors: false |
| 138 | +``` |
| 139 | +
|
| 140 | +### Example 4 - Using with other functions |
| 141 | +
|
| 142 | +The following example shows how to use `createObject()` with other DSC functions. |
| 143 | + |
| 144 | +```yaml |
| 145 | +# createObject.example.4.dsc.config.yaml |
| 146 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 147 | +parameters: |
| 148 | + userName: |
| 149 | + type: string |
| 150 | + defaultValue: guest |
| 151 | +resources: |
| 152 | +- name: Function integration |
| 153 | + type: Microsoft.DSC.Debug/Echo |
| 154 | + properties: |
| 155 | + output: |
| 156 | + userConfig: "[createObject('user', parameters('userName'), 'role', coalesce(null(), 'default'))]" |
| 157 | + fallbackObject: "[createObject('result', coalesce(null(), createObject('status', 'success')))]" |
| 158 | +``` |
| 159 | + |
| 160 | +```bash |
| 161 | +dsc config get --file createObject.example.4.dsc.config.yaml |
| 162 | +``` |
| 163 | + |
| 164 | +```yaml |
| 165 | +results: |
| 166 | +- name: Function integration |
| 167 | + type: Microsoft.DSC.Debug/Echo |
| 168 | + result: |
| 169 | + actualState: |
| 170 | + output: |
| 171 | + userConfig: |
| 172 | + user: guest |
| 173 | + role: default |
| 174 | + fallbackObject: |
| 175 | + result: |
| 176 | + status: success |
| 177 | +messages: [] |
| 178 | +hadErrors: false |
| 179 | +``` |
| 180 | + |
| 181 | +## Parameters |
| 182 | + |
| 183 | +### Key-value pairs |
| 184 | + |
| 185 | +The `createObject()` function accepts zero or more key-value pairs. Each key must be a string, |
| 186 | +and values can be of any type. |
| 187 | + |
| 188 | +```yaml |
| 189 | +Type: key: [string], value: [any] |
| 190 | +Required: false |
| 191 | +MinimumCount: 0 |
| 192 | +MaximumCount: unlimited (must be even number) |
| 193 | +``` |
| 194 | + |
| 195 | +#### key |
| 196 | + |
| 197 | +The object property name. Must be a string value. |
| 198 | + |
| 199 | +```yaml |
| 200 | +Type: [string] |
| 201 | +Required: true (when providing values) |
| 202 | +``` |
| 203 | + |
| 204 | +#### value |
| 205 | + |
| 206 | +The object property value. Can be any valid JSON type including strings, numbers, booleans, null, arrays, or other objects. |
| 207 | + |
| 208 | +```yaml |
| 209 | +Type: [any] |
| 210 | +Required: true (when providing keys) |
| 211 | +``` |
| 212 | + |
| 213 | +## Output |
| 214 | + |
| 215 | +The `createObject()` function returns a JSON object containing the specified key-value pairs. |
| 216 | + |
| 217 | +```yaml |
| 218 | +Type: [object] |
| 219 | +``` |
| 220 | + |
| 221 | +## Error conditions |
| 222 | + |
| 223 | +The function will return an error in the following cases: |
| 224 | + |
| 225 | +- **Odd number of arguments**: Arguments must be provided in key-value pairs |
| 226 | +- **Non-string keys**: All keys must be string values |
| 227 | +- **Invalid argument types**: Arguments must be valid JSON types |
| 228 | + |
| 229 | +## Notes |
| 230 | + |
| 231 | +- Keys must be strings. If you specify numeric or other types, DSC raises an error. |
| 232 | +- Values can be any valid JSON type including null, arrays, and nested objects. |
| 233 | +- Duplicate keys will result in the last value overwriting previous values. |
| 234 | +- Empty object creation (`createObject()` with no arguments) is supported. |
| 235 | +- The function preserves the order of properties as specified. |
| 236 | + |
| 237 | +## Related functions |
| 238 | + |
| 239 | +- [`createArray()`][00] - Creates arrays that can be used as object values |
| 240 | +- [`coalesce()`][01] - Provides fallback values for object properties |
| 241 | +- [`null()`][02] - Creates explicit null values for object properties |
| 242 | + |
| 243 | +<!-- Link reference definitions --> |
| 244 | +[00]: ./createArray.md |
| 245 | +[01]: ./coalesce.md |
| 246 | +[02]: ./null.md |
0 commit comments