PowerShell 5无Test-Json cmdlet时如何校验JSON文件匹配指定模板?
PowerShell 5中校验JSON结构的替代方案
由于PowerShell 5没有内置的Test-Json cmdlet,要校验JSON是否符合指定结构,可采用以下两种思路:
方法一:手动遍历属性校验(适合简单结构)
针对你给出的Schema要求(顶级必须包含buttons和Text键,buttons下固定1=Zenek、2=NetExt,Text下必须有1和2两个字符串键),可以通过将JSON转为PowerShell对象后,逐一校验属性和值:
# 定义预期的校验规则 $expectedTopKeys = @('buttons', 'Text') $expectedButtons = @{'1' = 'Zenek'; '2' = 'NetExt'} $expectedTextKeys = @('1', '2') # 待校验的JSON内容(实际使用时可替换为 Get-Content -Path '目标文件.json' -Raw) $jsonContent = @" { "buttons":{ "1":"Zenek", "2":"NetExt" }, "Text":{ "1":"THIS IS OK", "2":"CORRECT" } } "@ # 第一步:验证JSON格式是否有效 try { $jsonObj = ConvertFrom-Json -InputObject $jsonContent -ErrorAction Stop } catch { Write-Error "JSON格式错误:$_" exit 1 } # 第二步:校验顶级键是否匹配 $currentTopKeys = $jsonObj | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name if ((Compare-Object $expectedTopKeys $currentTopKeys -SyncWindow 0).Count -ne 0) { Write-Error "顶级键不匹配,预期:$($expectedTopKeys -join ', '),实际:$($currentTopKeys -join ', ')" exit 1 } # 第三步:校验buttons的键和值 $currentButtonsKeys = $jsonObj.buttons | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name if ((Compare-Object $expectedButtons.Keys $currentButtonsKeys -SyncWindow 0).Count -ne 0) { Write-Error "buttons子键不匹配,预期:$($expectedButtons.Keys -join ', '),实际:$($currentButtonsKeys -join ', ')" exit 1 } foreach ($key in $expectedButtons.Keys) { if ($jsonObj.buttons.$key -ne $expectedButtons[$key]) { Write-Error "buttons.$key值错误,预期:$($expectedButtons[$key]),实际:$($jsonObj.buttons.$key)" exit 1 } } # 第四步:校验Text的键是否存在 $currentTextKeys = $jsonObj.Text | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name if ((Compare-Object $expectedTextKeys $currentTextKeys -SyncWindow 0).Count -ne 0) { Write-Error "Text子键不匹配,预期:$($expectedTextKeys -join ', '),实际:$($currentTextKeys -join ', ')" exit 1 } Write-Host "JSON校验通过"
方法二:使用Newtonsoft.Json库进行Schema校验(适合复杂结构)
如果需要更专业的Schema校验(比如支持更多类型约束、额外属性限制等),可以借助Newtonsoft.Json库(需自行下载对应版本的dll):
# 加载Newtonsoft.Json库(替换为你的dll实际路径) Add-Type -Path "C:\Tools\Newtonsoft.Json.dll" Add-Type -Path "C:\Tools\Newtonsoft.Json.Schema.dll" # 定义符合JSON Schema规范的校验规则 $schemaJson = @" { "type": "object", "required": ["buttons", "Text"], "additionalProperties": false, "properties": { "buttons": { "type": "object", "required": ["1", "2"], "additionalProperties": false, "properties": { "1": {"type": "string", "const": "Zenek"}, "2": {"type": "string", "const": "NetExt"} } }, "Text": { "type": "object", "required": ["1", "2"], "additionalProperties": false, "properties": { "1": {"type": "string"}, "2": {"type": "string"} } } } } "@ # 待校验的JSON内容 $jsonContent = @" { "buttons":{ "1":"Zenek", "2":"NetExt" }, "Text":{ "1":"THIS IS OK", "2":"CORRECT" } } "@ try { # 解析Schema和待校验JSON $schema = [Newtonsoft.Json.Schema.JsonSchema]::Parse($schemaJson) $jsonReader = [Newtonsoft.Json.JsonTextReader]::New([System.IO.StringReader]::New($jsonContent)) $validator = [Newtonsoft.Json.Schema.JsonSchemaValidator]::New($schema) # 执行校验 while ($jsonReader.Read()) { $validator.Validate($jsonReader) } # 输出结果 if ($validator.IsValid) { Write-Host "JSON符合Schema要求" } else { Write-Error "Schema校验失败:$($validator.Errors.Message -join '; ')" } } catch { Write-Error "校验过程出错:$_" }
内容的提问来源于stack exchange,提问作者Nawad-sama
相关产品推荐
相关产品推荐

