Priyank Kumar Dalal
Priyank Kumar Dalal

Reputation: 117

How to validate JSON syntax in Powershell before parsing JSON file

I am trying to validate the file as JSON complaint, before starting my parsing script. I am trying to catch a Duplicate key or if file is not valid JSOn file. But it seems not working, any help pls:

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
    } 

It always says the file is valid JSON, even the file contains the duplicate key. 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 : Provided text has been correctly parsed to 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


Sample :

{
  "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
  }]
}

Upvotes: 2

Views: 3078

Answers (1)

JPBlanc
JPBlanc

Reputation: 72610

According to Does JSON syntax allow duplicate keys in an object? answer : two time the same key at the same level is not necessarily an error.

The .NET deserializer ("System.Web.Script.Serialization.JavaScriptSerializer" ?) does not detect it as an error, it just keep the second value.

If you try to use the same key but with a case sensitive difference, you will get an error.

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

Upvotes: 1

Related Questions