Skip to content

Commit ca5fefe

Browse files
authored
Merge pull request #1190 from Gijsreyn/gh-57/main/add-json-function
Add `json()` function
2 parents 24d34b8 + 153270f commit ca5fefe

File tree

5 files changed

+446
-0
lines changed

5 files changed

+446
-0
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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

dsc/tests/dsc_functions.tests.ps1

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,4 +1060,60 @@ Describe 'tests for function expressions' {
10601060
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
10611061
$out.results[0].result.actualState.output | Should -BeExactly $expected
10621062
}
1063+
1064+
It 'json() works: <accessor>' -TestCases @(
1065+
@{ data = @{ name = 'John'; age = 30 }; accessor = '.name'; expected = 'John' }
1066+
@{ data = @{ name = 'John'; age = 30 }; accessor = '.age'; expected = 30 }
1067+
@{ data = @(1,2,3); accessor = '[0]'; expected = 1 }
1068+
@{ data = @(1,2,3); accessor = '[2]'; expected = 3 }
1069+
@{ data = 'hello'; accessor = ''; expected = 'hello' }
1070+
@{ data = 42; accessor = ''; expected = 42 }
1071+
@{ data = $true; accessor = ''; expected = $true }
1072+
@{ data = $false; accessor = ''; expected = $false }
1073+
@{ data = $null; accessor = ''; expected = $null }
1074+
@{ data = @{ users = @( @{ name = 'Alice' }, @{ name = 'Bob' } ) }; accessor = '.users[0].name'; expected = 'Alice' }
1075+
@{ data = @{ users = @( @{ name = 'Alice' }, @{ name = 'Bob' } ) }; accessor = '.users[1].name'; expected = 'Bob' }
1076+
@{ data = @{ key = 'value' }; accessor = '.key'; expected = 'value' }
1077+
@{ data = @{ nested = @{ value = 123 } }; accessor = '.nested.value'; expected = 123 }
1078+
) {
1079+
param($data, $accessor, $expected)
1080+
1081+
$jsonString = ConvertTo-Json -Compress -InputObject $data
1082+
$expression = "[json(''$($jsonString)'')$accessor]"
1083+
1084+
$config_yaml = @"
1085+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
1086+
resources:
1087+
- name: Echo
1088+
type: Microsoft.DSC.Debug/Echo
1089+
properties:
1090+
output: '$expression'
1091+
"@
1092+
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
1093+
$out.results[0].result.actualState.output | Should -Be $expected
1094+
}
1095+
1096+
It 'json() error handling: <expression>' -TestCases @(
1097+
@{ expression = "[json('not valid json')]" }
1098+
@{ expression = "[json('{""key"":""value""')]" }
1099+
@{ expression = "[json('')]" }
1100+
@{ expression = "[json('{incomplete')]" }
1101+
@{ expression = "[json('[1,2,')]" }
1102+
) {
1103+
param($expression)
1104+
1105+
$escapedExpression = $expression -replace "'", "''"
1106+
$config_yaml = @"
1107+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
1108+
resources:
1109+
- name: Echo
1110+
type: Microsoft.DSC.Debug/Echo
1111+
properties:
1112+
output: '$escapedExpression'
1113+
"@
1114+
$null = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log
1115+
$LASTEXITCODE | Should -Not -Be 0
1116+
$errorContent = Get-Content $TestDrive/error.log -Raw
1117+
$errorContent | Should -Match ([regex]::Escape('Invalid JSON string'))
1118+
}
10631119
}

lib/dsc-lib/locales/en-us.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ invalidNullElement = "Array elements cannot be null"
384384
invalidArrayElement = "Array elements cannot be arrays"
385385
invalidObjectElement = "Array elements cannot be objects"
386386

387+
[functions.json]
388+
description = "Converts a valid JSON string into a JSON data type"
389+
invalidJson = "Invalid JSON string"
390+
387391
[functions.lastIndexOf]
388392
description = "Returns the index of the last occurrence of an item in an array"
389393
invoked = "lastIndexOf function"

0 commit comments

Comments
 (0)