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

該 cmdlet 測試 Test-Json 字串是否為有效的 JavaScript 物件標記(JSON)文件,並可選擇性地驗證該 JSON 文件是否符合所提供的結構。

驗證過的字串可與 ConvertFrom-Json cmdlet 一起使用,將 JSON 格式字串轉換為 JSON 物件,這在 PowerShell 中可輕鬆管理,或傳送至其他存取 JSON 輸入的程式或網路服務。

許多網站會使用 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。 這個參數允許你指定選項來更改預設行為。 下列是可用的選項:

  • IgnoreComments
  • AllowTrailingCommas

此參數是在 PowerShell 7.5.0-preview.4 中新增的。

參數屬性

類型:

String[]

預設值:None
接受的值:IgnoreComments, AllowTrailingCommas
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-Path

指定 JSON 檔案的路徑。 這個指令子在指定位置取得物品。 允許使用萬用字元,但模式必須解析為單一檔案。

此參數已在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 參數所指定的規範,且僅在輸入符合所提供 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 輸入符合 SchemaFile 參數所定義的結構時,才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 使用 Newtonsoft.Json 組合語言來處理 JSON 函式。 Newtonsoft 的實作包含多項 JSON 標準的擴充,例如支援註解與單引號的使用。 完整功能列表請參閱 Newtonsoft 的文件。https://www.newtonsoft.com/json

從 PowerShell 7.4 開始,Test-Json 使用 System.Text.Json進行 JSON 解析,並使用 JsonSchema.NET進行結構驗證。 經過這些變更後,: Test-Json

  • 不再支援第四版架構
  • 只支援嚴格符合規範的 JSON

關於 Newtonsoft.Json 與 System.Text.Json 之間的完整差異清單,請參閱「從 Newtonsoft.Json 遷移到 System.Text.Json 的差異表」。

欲了解更多關於 JSON 架構規範的資訊,請參閱 JSON-Schema.org 的文件。