I would like to validate my Json against a Json Schema in Windows PowerShell
For PowerShell 7, you might use Test-Json
for this:
$Json = @'
{
"name": "James",
"hobbies": [".NET", "Blogging", "Reading", "Xbox", "LOLCATS"]
}
'@
$JsonSchema = @'
{
"description": "A person",
"type": "object",
"required": ["name", "hobbies"],
"properties":
{
"name": {"type":"string"},
"hobbies": {
"type": "array",
"items": {"type":"string"}
}
}
}
'@
Test-Json -Json $Json -Schema $JsonSchema
True
But when I try to do something similar with the Newtonsoft.Json class (standard available in PowerShell 7), I get an error:
$Schema = [Newtonsoft.Json.Schema.JsonSchema]::Parse($JsonSchema)
$JObject = [Newtonsoft.Json.Linq.JObject]::Parse($Json)
$JObject.IsValid($Schema)
OperationStopped: C:\Users\Gebruiker\GDrive\Scripts\PowerShell\_JIO\Yaml\Test-Yaml.ps1:31
Line |
31 | $JObject.IsValid($Schema)
| ~~~~~~~~~~~~~~~~~~~~~~~~~
| Target type System.Collections.IEnumerator is not a value type or a non-abstract class. (Parameter 'targetType')
How can I fix this or workaround this?
References: