【问题标题】:How to validate JSON syntax in Powershell before parsing JSON file如何在解析 JSON 文件之前在 Powershell 中验证 JSON 语法
【发布时间】:2017-07-05 09:12:12
【问题描述】:

在开始我的解析脚本之前,我正在尝试将文件验证为 JSON 投诉。我正在尝试捕获重复键或者文件不是有效的 JSOn 文件。但它似乎不起作用,任何帮助请:

function ParseFile([string]$file, [int]$domainNumber)
{
    # read entire JSON file and parse

    $bytes = [system.io.file]::ReadAllText($file)
    $json = ConvertFrom-Json $bytes

    $text = Get-Content $file -Raw 

    try {
        $powershellRepresentation = ConvertFrom-Json $text -ErrorAction Stop;       
        $validJson = $true;
        Write-Error "IN TRY";
    } catch {
        Write-Error "IN CATCH";
        $validJson = $false;
    }


    if ($validJson) {
        Write-Error "Provided text has been correctly parsed to JSON";
        Exit 1
    } else {
        Write-Error "Provided text is not a JSON valid string";
        Exit 1
    } 

它总是说文件是有效的 JSON,即使文件包含重复键。 CALL powershell.exe -noprofile -executionpolicy bypass -file D:\Tools\Scripts json_files\config.pkg.xml ParseFile : 111 At D:\Tools\Scripts\json.ps1:110 char:4 + ParseFile $file $domainNumber + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell .Commands.WriteErrorException,ParseFile

ParseFile : 2222 At D:\Tools\Scripts\json.ps1:110 char:4 + ParseFile $file $domainNumber + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,ParseFile

ParseFile : 提供的文本已正确解析为 JSON At D:\Tools\Scripts\json.ps1:110 char:4 + ParseFile $file $domainNumber + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,ParseFile

How to check if file has valid JSON syntax in Powershell


示例:

{
  "sr_version": {
    "major": 1,
    "minor": 1,
    "patch": 1
  },
  "sr_domain": {
    "soc": "msm",
    "domain": "Audio",
    "subdomain": "root",
    "qmi_instance_id": 74
  },
  "sr_domain": {
    "soc": "msm",
    "domain": "Audio",
    "subdomain": "root",
    "qmi_instance_id": 74
  },
  "sr_service": [{
    "provider": "tms",
    "service": "servreg",
    "service_data_valid": 0,
    "service_data": 0
  }]
}

【问题讨论】:

  • 查看在 powershell 中使用 JSON.NET 验证的示例。
  • 我检查了这些,但没有帮助。我试图在解析开始之前捕获 JSON 文件中的重复键。寻找可以检查文件的机制,让我知道它是否是 JSON 标准。
  • 你能给我们看一个 JSON 样本吗?
  • { "sr_version": { "major": 1, "minor": 1, "patch": 1 }, "sr_domain": { "soc": "msm", "domain": “音频”、“子域”:“root”、“qmi_instance_id”:74 }、“sr_domain”:{ “soc”:“msm”、“域”:“音频”、“子域”:“root”、“qmi_instance_id” ": 74 }, "sr_service": [{ "provider": "tms", "service": "servreg", "service_data_valid": 0, "service_data": 0 }] } 上面的 JSON 文件 SR_domain 被重复不符合 JSON 验证器

标签: .net json powershell standards


【解决方案1】:

根据Does JSON syntax allow duplicate keys in an object?的回答:两次同级同一个key不一定是错误。

.NET 反序列化程序(“System.Web.Script.Serialization.JavaScriptSerializer”?)不会将其检测为错误,它只会保留第二个值。

如果您尝试使用相同的密钥,但区分大小写,则会出错。

ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys 'Key' and 'key'.
At C:\Temp\Untitled8.ps1:27 char:11
+ $b = $a | ConvertFrom-Json
+           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [ConvertFrom-Json], InvalidOperationException
    + FullyQualifiedErrorId : DuplicateKeysInJsonString,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

【讨论】:

  • 但是我们有什么解决方案可以捕获不区分大小写的重复键吗?
  • 我不这么认为,因为标准中包含重复键。
猜你喜欢
  • 2020-08-16
  • 2013-04-25
  • 1970-01-01
  • 2018-10-10
  • 2011-12-12
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
相关资源
最近更新 更多