Test-Json
Tests whether a string is a valid JSON document
Syntax
JsonString (Default)
Test-Json
[-Json] <String>
[-Options <String[]>]
[<CommonParameters>]
JsonStringWithSchemaString
Test-Json
[-Json] <String>
[-Schema] <String>
[-Options <String[]>]
[<CommonParameters>]
JsonStringWithSchemaFile
Test-Json
[-Json] <String>
-SchemaFile <String>
[-Options <String[]>]
[<CommonParameters>]
JsonPath
Test-Json
-Path <String>
[-Options <String[]>]
[<CommonParameters>]
JsonPathWithSchemaString
Test-Json
[-Schema] <String>
-Path <String>
[-Options <String[]>]
[<CommonParameters>]
JsonPathWithSchemaFile
Test-Json
-Path <String>
-SchemaFile <String>
[-Options <String[]>]
[<CommonParameters>]
JsonLiteralPath
Test-Json
-LiteralPath <String>
[-Options <String[]>]
[<CommonParameters>]
JsonLiteralPathWithSchemaString
Test-Json
[-Schema] <String>
-LiteralPath <String>
[-Options <String[]>]
[<CommonParameters>]
JsonLiteralPathWithSchemaFile
Test-Json
-LiteralPath <String>
-SchemaFile <String>
[-Options <String[]>]
[<CommonParameters>]
Description
The Test-Json cmdlet tests whether a string is a valid JavaScript Object Notation (JSON) document
and can optionally verify that JSON document against a provided schema.
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.
Many web sites use JSON instead of XML to serialize data for communication between servers and web-based apps.
This cmdlet was introduced in PowerShell 6.1
Examples
Example 1: Test if an object is valid JSON
This example tests whether the input string is a valid JSON document.
'{"name": "Ashley", "age": 25}' | Test-Json
True
Example 2: Test an object against a provided schema
This example takes a string containing a JSON schema and compares it to an input string.
$schema = @'
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"name",
"age"
],
"properties": {
"name": {
"$id": "#/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"Ashley"
],
"pattern": "^(.*)$"
},
"age": {
"$id": "#/properties/age",
"type": "integer",
"title": "The Age Schema",
"default": 0,
"examples": [
25
]
}
}
}
'@
'{"name": "Ashley", "age": "25"}' | Test-Json -Schema $schema -ErrorAction SilentlyContinue
False
In this example, we use the ErrorAction parameter to suppress the error message. Without this parameter the command also outputs an error because the schema expects an integer for age but the JSON input we tested uses a string value instead.
For more information, see JSON Schema.
Example 3: Test an object against a schema from file
JSON schema can reference definitions using $ref keyword. The $ref can resolve to a URI that
references another file. The SchemaFile parameter accepts literal path to the JSON schema file
and allows JSON files to be validated against such schemas.
In this example the schema.json file references definitions.json.
Get-Content schema.json
{
"description":"A person",
"type":"object",
"properties":{
"name":{
"$ref":"definitions.json#/definitions/name"
},
"hobbies":{
"$ref":"definitions.json#/definitions/hobbies"
}
}
}
Get-Content definitions.json
{
"definitions":{
"name":{
"type":"string"
},
"hobbies":{
"type":"array",
"items":{
"type":"string"
}
}
}
}
'{"name": "James", "hobbies": [".NET", "Blogging"]}' | Test-Json -SchemaFile 'schema.json'
True
For more information, see Structuring a complex schema.
Parameters
-Json
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.
The Json parameter is required.
Parameter properties
| Type: | String |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
JsonString
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | True |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
JsonStringWithSchemaString
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | True |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
JsonStringWithSchemaFile
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | True |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-LiteralPath
Specifies a path to a JSON file. The value of LiteralPath is used exactly as it's typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences.
This parameter was added in PowerShell 7.4.
Parameter properties
| Type: | String |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
| Aliases: | PSPath, LP |
Parameter sets
JsonLiteralPath
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
JsonLiteralPathWithSchemaString
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
JsonLiteralPathWithSchemaFile
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
-Options
By default, Test-Json doesn't support JSON containing comments or trailing commas. This parameter
allows you to specify options to change the default behavior. The following options are available:
IgnoreCommentsAllowTrailingCommas
This parameter was added in PowerShell 7.5.0-preview.4.
Parameter properties
| Type: | String[] |
| Default value: | None |
| Accepted values: | IgnoreComments, AllowTrailingCommas |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
(All)
| Position: | Named |
| Mandatory: | False |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-Path
Specifies the path to a JSON file. This cmdlet gets the item at the specified location. Wildcard characters are permitted but the pattern must resolve to a single file.
This parameter was added in PowerShell 7.4.
Parameter properties
| Type: | String |
| Default value: | None |
| Supports wildcards: | True |
| DontShow: | False |
Parameter sets
JsonPath
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
JsonPathWithSchemaString
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
JsonPathWithSchemaFile
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
-Schema
Specifies a schema to validate the JSON input against. If passed, Test-Json validates 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.
For more information, see JSON Schema.
Parameter properties
| Type: | String |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
JsonStringWithSchemaString
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
JsonLiteralPathWithSchemaString
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
JsonPathWithSchemaString
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-SchemaFile
Specifies a schema file used to validate the JSON input. When used, the Test-Json returns $true
only if the JSON input conforms to the schema defined in the file specified by the SchemaFile
parameter.
For more information, see JSON Schema.
Parameter properties
| Type: | String |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
JsonStringWithSchemaFile
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
JsonLiteralPathWithSchemaFile
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
JsonPathWithSchemaFile
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
Inputs
String
You can pipe a JSON string to this cmdlet.
Outputs
Boolean
This cmdlet returns $true if the JSON is valid and otherwise $false.
Notes
Since PowerShell 6, PowerShell uses the Newtonsoft.Json assemblies for JSON functions. Newtonsoft's implementation includes several extensions to the JSON standard, such as support for comments and use of single quotes. For a full list of features, see the Newtonsoft documentation at https://www.newtonsoft.com/json.
Beginning in PowerShell 7.4, Test-Json uses System.Text.Json for JSON
parsing and JsonSchema.NET for schema validation.
With these changes, Test-Json:
- No longer supports Draft 4 schemas
- Only supports strictly conformant JSON
For a complete list of differences between Newtonsoft.Json and System.Text.Json, see the Table of differences in Migrate from Newtonsoft.Json to System.Text.Json.
For more information about JSON schema specifications, see the documentation at JSON-Schema.org.