|
| 1 | +--- |
| 2 | +description: Reference for the 'json' DSC configuration document function |
| 3 | +ms.date: 10/11/2025 |
| 4 | +ms.topic: reference |
| 5 | +title: json |
| 6 | +--- |
| 7 | + |
| 8 | +## Synopsis |
| 9 | + |
| 10 | +Converts a valid JSON string into a JSON data type. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```Syntax |
| 15 | +json(arg1) |
| 16 | +``` |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +The `json()` function parses a JSON string and returns the corresponding JSON data type. |
| 21 | + |
| 22 | +- The string must be a properly formatted JSON string. |
| 23 | +- Returns the parsed JSON value (object, array, string, number, boolean, or null). |
| 24 | + |
| 25 | +This function is useful for converting JSON strings received from external sources or |
| 26 | +stored as configuration into usable data structures. |
| 27 | + |
| 28 | +## Examples |
| 29 | + |
| 30 | +### Example 1 - Parse JSON object |
| 31 | + |
| 32 | +This example parses a JSON string containing an object into a usable object data type. |
| 33 | + |
| 34 | +```yaml |
| 35 | +# json.example.1.dsc.config.yaml |
| 36 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 37 | +resources: |
| 38 | +- name: Echo |
| 39 | + type: Microsoft.DSC.Debug/Echo |
| 40 | + properties: |
| 41 | + output: "[json('{\"name\":\"John\",\"age\":30}')]" |
| 42 | +``` |
| 43 | +
|
| 44 | +```bash |
| 45 | +dsc config get --file json.example.1.dsc.config.yaml |
| 46 | +``` |
| 47 | + |
| 48 | +```yaml |
| 49 | +results: |
| 50 | +- name: Echo |
| 51 | + type: Microsoft.DSC.Debug/Echo |
| 52 | + result: |
| 53 | + actualState: |
| 54 | + output: |
| 55 | + name: John |
| 56 | + age: 30 |
| 57 | +messages: [] |
| 58 | +hadErrors: false |
| 59 | +``` |
| 60 | +
|
| 61 | +### Example 2 - Parse JSON array |
| 62 | +
|
| 63 | +This example parses a JSON string containing an array using [`json()`][00] and then |
| 64 | +uses [`length()`][01] to count the elements. |
| 65 | + |
| 66 | +```yaml |
| 67 | +# json.example.2.dsc.config.yaml |
| 68 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 69 | +resources: |
| 70 | +- name: Echo |
| 71 | + type: Microsoft.DSC.Debug/Echo |
| 72 | + properties: |
| 73 | + output: "[length(json('[1,2,3,4,5]'))]" |
| 74 | +``` |
| 75 | + |
| 76 | +```bash |
| 77 | +dsc config get --file json.example.2.dsc.config.yaml |
| 78 | +``` |
| 79 | + |
| 80 | +```yaml |
| 81 | +results: |
| 82 | +- name: Echo |
| 83 | + type: Microsoft.DSC.Debug/Echo |
| 84 | + result: |
| 85 | + actualState: |
| 86 | + output: 5 |
| 87 | +messages: [] |
| 88 | +hadErrors: false |
| 89 | +``` |
| 90 | + |
| 91 | +### Example 3 - Parse nested JSON structure |
| 92 | + |
| 93 | +This example parses a JSON string with nested objects and arrays, then accesses nested |
| 94 | +properties using array indexing and property access. |
| 95 | + |
| 96 | +```yaml |
| 97 | +# json.example.3.dsc.config.yaml |
| 98 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 99 | +resources: |
| 100 | +- name: Echo |
| 101 | + type: Microsoft.DSC.Debug/Echo |
| 102 | + properties: |
| 103 | + output: "[json('{\"users\":[{\"name\":\"Alice\"},{\"name\":\"Bob\"}]}').users[0].name]" |
| 104 | +``` |
| 105 | + |
| 106 | +```bash |
| 107 | +dsc config get --file json.example.3.dsc.config.yaml |
| 108 | +``` |
| 109 | + |
| 110 | +```yaml |
| 111 | +results: |
| 112 | +- name: Echo |
| 113 | + type: Microsoft.DSC.Debug/Echo |
| 114 | + result: |
| 115 | + actualState: |
| 116 | + output: Alice |
| 117 | +messages: [] |
| 118 | +hadErrors: false |
| 119 | +``` |
| 120 | + |
| 121 | +### Example 4 - Parse JSON with whitespace |
| 122 | + |
| 123 | +This example shows that `json()` handles JSON strings with extra whitespace. |
| 124 | + |
| 125 | +```yaml |
| 126 | +# json.example.4.dsc.config.yaml |
| 127 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 128 | +resources: |
| 129 | +- name: Echo |
| 130 | + type: Microsoft.DSC.Debug/Echo |
| 131 | + properties: |
| 132 | + output: "[json(' { \"key\" : \"value\" } ').key]" |
| 133 | +``` |
| 134 | + |
| 135 | +```bash |
| 136 | +dsc config get --file json.example.4.dsc.config.yaml |
| 137 | +``` |
| 138 | + |
| 139 | +```yaml |
| 140 | +results: |
| 141 | +- name: Echo |
| 142 | + type: Microsoft.DSC.Debug/Echo |
| 143 | + result: |
| 144 | + actualState: |
| 145 | + output: value |
| 146 | +messages: [] |
| 147 | +hadErrors: false |
| 148 | +``` |
| 149 | + |
| 150 | +### Example 5 - Parse primitive JSON values |
| 151 | + |
| 152 | +This example demonstrates parsing different primitive JSON values including strings, |
| 153 | +numbers, and booleans. |
| 154 | + |
| 155 | +```yaml |
| 156 | +# json.example.5.dsc.config.yaml |
| 157 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 158 | +resources: |
| 159 | +- name: Echo string |
| 160 | + type: Microsoft.DSC.Debug/Echo |
| 161 | + properties: |
| 162 | + output: "[json('\"hello\"')]" |
| 163 | +- name: Echo number |
| 164 | + type: Microsoft.DSC.Debug/Echo |
| 165 | + properties: |
| 166 | + output: "[json('42')]" |
| 167 | +- name: Echo boolean |
| 168 | + type: Microsoft.DSC.Debug/Echo |
| 169 | + properties: |
| 170 | + output: "[json('true')]" |
| 171 | +``` |
| 172 | + |
| 173 | +```bash |
| 174 | +dsc config get --file json.example.5.dsc.config.yaml |
| 175 | +``` |
| 176 | + |
| 177 | +```yaml |
| 178 | +results: |
| 179 | +- name: Echo string |
| 180 | + type: Microsoft.DSC.Debug/Echo |
| 181 | + result: |
| 182 | + actualState: |
| 183 | + output: hello |
| 184 | +- name: Echo number |
| 185 | + type: Microsoft.DSC.Debug/Echo |
| 186 | + result: |
| 187 | + actualState: |
| 188 | + output: 42 |
| 189 | +- name: Echo boolean |
| 190 | + type: Microsoft.DSC.Debug/Echo |
| 191 | + result: |
| 192 | + actualState: |
| 193 | + output: true |
| 194 | +messages: [] |
| 195 | +hadErrors: false |
| 196 | +``` |
| 197 | + |
| 198 | +## Parameters |
| 199 | + |
| 200 | +### arg1 |
| 201 | + |
| 202 | +The JSON string to parse. Must be a properly formatted JSON string. |
| 203 | + |
| 204 | +```yaml |
| 205 | +Type: string |
| 206 | +Required: true |
| 207 | +Position: 1 |
| 208 | +``` |
| 209 | + |
| 210 | +## Output |
| 211 | + |
| 212 | +Returns the parsed JSON value. The type depends on the JSON content: |
| 213 | + |
| 214 | +- Object for JSON objects |
| 215 | +- Array for JSON arrays |
| 216 | +- String for JSON strings |
| 217 | +- Number for JSON numbers |
| 218 | +- Boolean for JSON booleans |
| 219 | +- Null for JSON null |
| 220 | + |
| 221 | +```yaml |
| 222 | +Type: object | array | string | number | boolean | null |
| 223 | +``` |
| 224 | + |
| 225 | +## Related functions |
| 226 | + |
| 227 | +- [`length()`][00] - Returns the length of an array or object |
| 228 | +- [`string()`][01] - Converts values to strings |
| 229 | + |
| 230 | +<!-- Link reference definitions --> |
| 231 | +[00]: ./length.md |
| 232 | +[01]: ./string.md |
0 commit comments