Skip to content

Commit 76b6108

Browse files
sdwheelerDCtheGeek
authored andcommitted
Fixes #4809 - style cleanup (#4810)
* Fixes #4809 - style cleanup * fix typos * updated example 2 * fix missing output
1 parent 9a9ea99 commit 76b6108

File tree

2 files changed

+154
-98
lines changed

2 files changed

+154
-98
lines changed

reference/6/Microsoft.PowerShell.Utility/Test-Json.md

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
ms.date: 10/10/2018
2+
ms.date: 09/19/2019
33
schema: 2.0.0
44
locale: en-us
55
keywords: powershell,cmdlet
@@ -20,72 +20,98 @@ Test-Json [-Json] <string> [[-Schema] <string>] [<CommonParameters>]
2020
```
2121

2222
## DESCRIPTION
23-
The Test-Json cmdlet tests whether a string is a valid JavaScript Object Notation (JSON) document and can optionally very that JSON document against a provided schema.
2423

25-
The verified string can then be used with the ConvertFrom-Json cmdlet convert a JSON-formatted string to a JSON object, which is easily managed in PowerShell or sent to another program or web service that access JSON input.
24+
The `Test-Json` cmdlet tests whether a string is a valid JavaScript Object Notation (JSON) document
25+
and can optionally very that JSON document against a provided schema.
2626

27-
Many web sites use JSON instead of XML to serialize data for communication between servers and web-based apps.
27+
The verified string can then be used with the `ConvertFrom-Json` cmdlet convert a JSON-formatted
28+
string to a JSON object, which is easily managed in PowerShell or sent to another program or web
29+
service that access JSON input.
2830

29-
This cmdlet was introduced in Windows PowerShell 6.1
31+
Many web sites use JSON instead of XML to serialize data for communication between servers and
32+
web-based apps.
33+
34+
This cmdlet was introduced in PowerShell 6.1
3035

3136
## EXAMPLES
3237

3338
### Example 1: Test if an object is valid JSON
39+
40+
This example tests whether the input string is a valid JSON document.
41+
42+
```powershell
43+
"{'name': 'Ashley', 'age': 25}" | Test-Json
3444
```
35-
PS C:\> "{'name': 'Ashley', 'age': 25}" | Test-Json
45+
46+
```Output
3647
True
3748
```
38-
This command tests whether the input string is a valid JSON document, returning True since the string is valid JSON.
3949

4050
### Example 2: Test an object against a provided schema
51+
52+
This example takes a string containing a JSON schema and compares it to an input string.
53+
4154
```powershell
42-
PS C:\> $schema = '{
43-
>> "definitions": {},
44-
>> "$schema": "http://json-schema.org/draft-07/schema#",
45-
>> "$id": "http://example.com/root.json",
46-
>> "type": "object",
47-
>> "title": "The Root Schema",
48-
>> "required": [
49-
>> "name",
50-
>> "age"
51-
>> ],
52-
>> "properties": {
53-
>> "name": {
54-
>> "$id": "#/properties/name",
55-
>> "type": "string",
56-
>> "title": "The Name Schema",
57-
>> "default": "",
58-
>> "examples": [
59-
>> "Ashley"
60-
>> ],
61-
>> "pattern": "^(.*)$"
62-
>> },
63-
>> "age": {
64-
>> "$id": "#/properties/age",
65-
>> "type": "integer",
66-
>> "title": "The Age Schema",
67-
>> "default": 0,
68-
>> "examples": [
69-
>> 25
70-
>> ]
71-
>> }
72-
>> }
73-
>> }'
74-
PS C:\> "{'name': 'Ashley', 'age': 25}" | Test-Json -schema $schema
75-
True
55+
$schema = @'
56+
{
57+
"definitions": {},
58+
"$schema": "http://json-schema.org/draft-07/schema#",
59+
"$id": "http://example.com/root.json",
60+
"type": "object",
61+
"title": "The Root Schema",
62+
"required": [
63+
"name",
64+
"age"
65+
],
66+
"properties": {
67+
"name": {
68+
"$id": "#/properties/name",
69+
"type": "string",
70+
"title": "The Name Schema",
71+
"default": "",
72+
"examples": [
73+
"Ashley"
74+
],
75+
"pattern": "^(.*)$"
76+
},
77+
"age": {
78+
"$id": "#/properties/age",
79+
"type": "integer",
80+
"title": "The Age Schema",
81+
"default": 0,
82+
"examples": [
83+
25
84+
]
85+
}
86+
}
87+
}
88+
'@
89+
"{'name': 'Ashley', 'age': '25'}" | Test-Json -Schema $schema
90+
```
91+
92+
```Output
93+
Test-Json : IntegerExpected: #/age
94+
At line:1 char:37
95+
+ "{'name': 'Ashley', 'age': '25'}" | Test-Json -Schema $schema
96+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
97+
+ CategoryInfo : InvalidData: (:) [Test-Json], Exception
98+
+ FullyQualifiedErrorId : InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand
99+
False
76100
```
77101

78-
This Command takes a string containing a JSON schema and compares it to an input string. Since the input string conforms to the schema described in the -schema argument the cmdlet returns `$True`.
102+
In this example, we get an error because the schema expects an integer for **age** but the JSON
103+
input we tested uses a string value instead.
79104

80-
For more information, see [JSON Schema](https://json-schema.org/)
105+
For more information, see [JSON Schema](https://json-schema.org/).
81106

82107
## PARAMETERS
83108

84109
### -Json
85110

86-
Specifies the JSON string to test for validity. Enter a variable that contains the string, or type a command or expression that gets the string. You can also pipe a string to `Test-Json`
111+
Specifies the JSON string to test for validity. Enter a variable that contains the string, or type a
112+
command or expression that gets the string. You can also pipe a string to `Test-Json`.
87113

88-
The `Json` parameter is required.
114+
The **Json** parameter is required.
89115

90116
```yaml
91117
Type: String
@@ -101,9 +127,11 @@ Accept wildcard characters: False
101127
102128
### -Schema
103129
104-
Specifies a Schema to validate the JSON input against. If passed `Test-Json` will validate that the Json input conforms to the spec specified by the `-Schema` parameter and return `$True` only if the input conforms to the provided Schema.
130+
Specifies a Schema to validate the JSON input against. If passed `Test-Json` will validate that the
131+
Json input conforms to the spec specified by the **Schema** parameter and return `$True` only if the
132+
input conforms to the provided Schema.
105133

106-
For more information, see [JSON Schema](https://json-schema.org/)
134+
For more information, see [JSON Schema](https://json-schema.org/).
107135

108136
```yaml
109137
Type: String
@@ -135,11 +163,11 @@ You can pipe a JSON string to `Test-Json`.
135163

136164
## NOTES
137165

138-
The `Test-Json` cmdlet is implemented by using the [NJsonSchema Class](https://github.com/RSuter/NJsonSchema)
166+
The `Test-Json` cmdlet is implemented by using the [NJsonSchema Class](https://github.com/RSuter/NJsonSchema).
139167

140168
## RELATED LINKS
141169

142-
[An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET](https://msdn.microsoft.com/en-us/library/bb299886.aspx)
170+
[An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET](/previous-versions/dotnet/articles/bb299886(v=msdn.10))
143171

144172
[Additional JSON Schema Details](https://json-schema.org/)
145173

reference/7/Microsoft.PowerShell.Utility/Test-Json.md

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
ms.date: 10/10/2018
2+
ms.date: 09/19/2019
33
schema: 2.0.0
44
locale: en-us
55
keywords: powershell,cmdlet
@@ -20,72 +20,98 @@ Test-Json [-Json] <string> [[-Schema] <string>] [<CommonParameters>]
2020
```
2121

2222
## DESCRIPTION
23-
The Test-Json cmdlet tests whether a string is a valid JavaScript Object Notation (JSON) document and can optionally very that JSON document against a provided schema.
2423

25-
The verified string can then be used with the ConvertFrom-Json cmdlet convert a JSON-formatted string to a JSON object, which is easily managed in PowerShell or sent to another program or web service that access JSON input.
24+
The `Test-Json` cmdlet tests whether a string is a valid JavaScript Object Notation (JSON) document
25+
and can optionally very that JSON document against a provided schema.
2626

27-
Many web sites use JSON instead of XML to serialize data for communication between servers and web-based apps.
27+
The verified string can then be used with the `ConvertFrom-Json` cmdlet convert a JSON-formatted
28+
string to a JSON object, which is easily managed in PowerShell or sent to another program or web
29+
service that access JSON input.
2830

29-
This cmdlet was introduced in Windows PowerShell 6.1
31+
Many web sites use JSON instead of XML to serialize data for communication between servers and
32+
web-based apps.
33+
34+
This cmdlet was introduced in PowerShell 6.1
3035

3136
## EXAMPLES
3237

3338
### Example 1: Test if an object is valid JSON
39+
40+
This example tests whether the input string is a valid JSON document.
41+
42+
```powershell
43+
"{'name': 'Ashley', 'age': 25}" | Test-Json
3444
```
35-
PS C:\> "{'name': 'Ashley', 'age': 25}" | Test-Json
45+
46+
```Output
3647
True
3748
```
38-
This command tests whether the input string is a valid JSON document, returning True since the string is valid JSON.
3949

4050
### Example 2: Test an object against a provided schema
51+
52+
This example takes a string containing a JSON schema and compares it to an input string.
53+
4154
```powershell
42-
PS C:\> $schema = '{
43-
>> "definitions": {},
44-
>> "$schema": "http://json-schema.org/draft-07/schema#",
45-
>> "$id": "http://example.com/root.json",
46-
>> "type": "object",
47-
>> "title": "The Root Schema",
48-
>> "required": [
49-
>> "name",
50-
>> "age"
51-
>> ],
52-
>> "properties": {
53-
>> "name": {
54-
>> "$id": "#/properties/name",
55-
>> "type": "string",
56-
>> "title": "The Name Schema",
57-
>> "default": "",
58-
>> "examples": [
59-
>> "Ashley"
60-
>> ],
61-
>> "pattern": "^(.*)$"
62-
>> },
63-
>> "age": {
64-
>> "$id": "#/properties/age",
65-
>> "type": "integer",
66-
>> "title": "The Age Schema",
67-
>> "default": 0,
68-
>> "examples": [
69-
>> 25
70-
>> ]
71-
>> }
72-
>> }
73-
>> }'
74-
PS C:\> "{'name': 'Ashley', 'age': 25}" | Test-Json -schema $schema
75-
True
55+
$schema = @'
56+
{
57+
"definitions": {},
58+
"$schema": "http://json-schema.org/draft-07/schema#",
59+
"$id": "http://example.com/root.json",
60+
"type": "object",
61+
"title": "The Root Schema",
62+
"required": [
63+
"name",
64+
"age"
65+
],
66+
"properties": {
67+
"name": {
68+
"$id": "#/properties/name",
69+
"type": "string",
70+
"title": "The Name Schema",
71+
"default": "",
72+
"examples": [
73+
"Ashley"
74+
],
75+
"pattern": "^(.*)$"
76+
},
77+
"age": {
78+
"$id": "#/properties/age",
79+
"type": "integer",
80+
"title": "The Age Schema",
81+
"default": 0,
82+
"examples": [
83+
25
84+
]
85+
}
86+
}
87+
}
88+
'@
89+
"{'name': 'Ashley', 'age': '25'}" | Test-Json -Schema $schema
90+
```
91+
92+
```Output
93+
Test-Json : IntegerExpected: #/age
94+
At line:1 char:37
95+
+ "{'name': 'Ashley', 'age': '25'}" | Test-Json -Schema $schema
96+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
97+
+ CategoryInfo : InvalidData: (:) [Test-Json], Exception
98+
+ FullyQualifiedErrorId : InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand
99+
False
76100
```
77101

78-
This Command takes a string containing a JSON schema and compares it to an input string. Since the input string conforms to the schema described in the -schema argument the cmdlet returns `$True`.
102+
In this example, we get an error because the schema expects an integer for **age** but the JSON
103+
input we tested uses a string value instead.
79104

80-
For more information, see [JSON Schema](https://json-schema.org/)
105+
For more information, see [JSON Schema](https://json-schema.org/).
81106

82107
## PARAMETERS
83108

84109
### -Json
85110

86-
Specifies the JSON string to test for validity. Enter a variable that contains the string, or type a command or expression that gets the string. You can also pipe a string to `Test-Json`
111+
Specifies the JSON string to test for validity. Enter a variable that contains the string, or type a
112+
command or expression that gets the string. You can also pipe a string to `Test-Json`.
87113

88-
The `Json` parameter is required.
114+
The **Json** parameter is required.
89115

90116
```yaml
91117
Type: String
@@ -101,9 +127,11 @@ Accept wildcard characters: False
101127
102128
### -Schema
103129
104-
Specifies a Schema to validate the JSON input against. If passed `Test-Json` will validate that the Json input conforms to the spec specified by the `-Schema` parameter and return `$True` only if the input conforms to the provided Schema.
130+
Specifies a Schema to validate the JSON input against. If passed `Test-Json` will validate that the
131+
Json input conforms to the spec specified by the **Schema** parameter and return `$True` only if the
132+
input conforms to the provided Schema.
105133

106-
For more information, see [JSON Schema](https://json-schema.org/)
134+
For more information, see [JSON Schema](https://json-schema.org/).
107135

108136
```yaml
109137
Type: String
@@ -135,11 +163,11 @@ You can pipe a JSON string to `Test-Json`.
135163

136164
## NOTES
137165

138-
The `Test-Json` cmdlet is implemented by using the [NJsonSchema Class](https://github.com/RSuter/NJsonSchema)
166+
The `Test-Json` cmdlet is implemented by using the [NJsonSchema Class](https://github.com/RSuter/NJsonSchema).
139167

140168
## RELATED LINKS
141169

142-
[An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET](https://msdn.microsoft.com/en-us/library/bb299886.aspx)
170+
[An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET](/previous-versions/dotnet/articles/bb299886(v=msdn.10))
143171

144172
[Additional JSON Schema Details](https://json-schema.org/)
145173

0 commit comments

Comments
 (0)