2

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
  }]
}
4
  • See examples of using JSON.NET validation in powershell. Commented Feb 16, 2017 at 10:18
  • I checked those, but it i snot helping. I am trying to catch Duplicate key in JSON file before my parsing starts. Looking for mechanism which can check the file and let me know whether it is JSON standard or not. Commented Feb 16, 2017 at 10:28
  • Can you show us a JSON sample? Commented Feb 16, 2017 at 10:39
  • { "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 }] } in above JSON file SR_domain is getting repeated which is not as per JSON validator Commented Feb 16, 2017 at 10:52

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

But do we have any solution which can catch the duplicate key without case sensitive?
I d'on't think so, as duplicate keys are included into the standard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.