Test-Json
測試字串是否為有效的 JSON 檔
語法
JsonString (預設值)
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
Test-Json Cmdlet 會測試字串是否為有效的 JavaScript 物件表示法 (JSON) 檔,並可選擇性地根據提供的架構驗證 JSON 檔。
然後,驗證的字串可以與 ConvertFrom-Json Cmdlet 搭配使用,將 JSON 格式的字串轉換成 JSON 物件,這可在 PowerShell 中輕鬆管理,或傳送至存取 JSON 輸入的另一個程式或 Web 服務。
許多網站會使用 JSON 而不是 XML 來串行化伺服器與 Web 應用程式之間的通訊資料。
此 Cmdlet 已在 PowerShell 6.1 中引進
範例
範例 1:測試物件是否為有效的 JSON
此範例會測試輸入字串是否為有效的 JSON 檔。
'{"name": "Ashley", "age": 25}' | Test-Json
True
範例 2:針對提供的架構測試物件
此範例會採用包含 JSON 架構的字串,並將它與輸入字串進行比較。
$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
在此範例中,我們使用 ErrorAction 參數來隱藏錯誤訊息。 如果沒有此參數,命令也會輸出錯誤,因為架構預期 存留期 為整數,但我們測試的 JSON 輸入會改用字串值。
如需詳細資訊,請參閱 JSON 架構。
範例 3:從檔案對架構測試物件
JSON 架構可以使用 $ref 關鍵詞來參考定義。
$ref 可以解析為參考另一個檔案的 URI。
SchemaFile 參數接受 JSON 架構檔案的常值路徑,並允許針對這類架構驗證 JSON 檔案。
在這裡範例中,schema.json 檔案參考 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
如需詳細資訊,請參閱 建構複雜架構。
參數
-Json
指定要測試有效性的 JSON 字串。 輸入包含字串的變數,或輸入取得字串的命令或表達式。 您也可以將字串透過管道傳送至 Test-Json。
需要 Json 參數。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
JsonString
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
JsonStringWithSchemaString
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
JsonStringWithSchemaFile
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-LiteralPath
指定 JSON 檔案的路徑。 LiteralPath 的值會被原樣使用,不做任何更改。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
此參數已在PowerShell 7.4中新增。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | PSPath, LP |
參數集
JsonLiteralPath
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
JsonLiteralPathWithSchemaString
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
JsonLiteralPathWithSchemaFile
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Options
根據預設,Test-Json 不支援包含批注或尾端逗號的 JSON。 此參數可讓您指定選項來變更預設行為。 有下列選項可供使用:
IgnoreCommentsAllowTrailingCommas
此參數已在 PowerShell 7.5.0-preview.4 中新增。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 接受的值: | IgnoreComments, AllowTrailingCommas |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Path
指定 JSON 檔案的路徑。 此 Cmdlet 會在指定的位置取得專案。 允許通配符,但模式必須解析為單一檔案。
此參數已在PowerShell 7.4中新增。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
JsonPath
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
JsonPathWithSchemaString
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
JsonPathWithSchemaFile
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Schema
指定要驗證 JSON 輸入的架構。 如果傳遞,Test-Json 會驗證 JSON 輸入是否符合 Schema 參數所指定的規格,而且只有在輸入符合提供的架構時,才會傳回 $true。
如需詳細資訊,請參閱 JSON 架構。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
JsonStringWithSchemaString
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
JsonLiteralPathWithSchemaString
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
JsonPathWithSchemaString
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-SchemaFile
指定用來驗證 JSON 輸入的架構檔案。 使用時,只有當 JSON 輸入符合 Test-Json 參數所指定的檔案中所定義的架構時,$true 才會傳回 。
如需詳細資訊,請參閱 JSON 架構。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
JsonStringWithSchemaFile
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
JsonLiteralPathWithSchemaFile
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
JsonPathWithSchemaFile
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
String
您可以使用管線將 JSON 字串傳送至此 Cmdlet。
輸出
Boolean
如果 JSON 有效,則此 Cmdlet 會傳回 $true,否則 $false。
備註
由於 PowerShell 6,PowerShell 會針對 JSON 函式使用 Newtonsoft.Json 元件。 Newtonsoft 的實作包含數個 JSON 標準的延伸模組,例如支援批注和使用單引號。 如需功能的完整清單,請參閱 https://www.newtonsoft.com/json的Newtonsoft檔。
從 PowerShell 7.4 開始,Test-Json 會針對 JSON 剖析使用 System.Text.Json,並針對架構驗證使用 JsonSchema.NET。
透過這些變更,Test-Json:
- 不再支援草稿 4 架構
- 僅支持嚴格符合規範的 JSON
如需 Newtonsoft.Json 與 System.Text.Json 之間差異的完整清單,請參閱 從 Newtonsoft.Json 移轉至 System.Text.Json中的 差異數據表。
如需 JSON 架構規格的詳細資訊,請參閱 JSON-Schema.org的檔。