PowerShell Module For JSON Schema Validation

2 minute read


Few days ago, I needed to validate JSON files against a predefined schema in a build pipeline in Azure DevOps. The validation needed to be performed using the Pester framework, and fail the build if the validation failed.

In the past, I’ve always used this script (https://gist.github.com/JamesNK/7e6d026c8b78c049bb1e1b6fb0ed85cf) from James Newton-King, which leverages the JSON.Net libraries he developed. However, this time, I couldn’t it the script working on my Windows 10 laptop. I tried different versions of the DLLs, some won’t load, and the version that loads fine on my laptop threw some errors about System.Runtime library not referenced in the C# code he embedded in the PowerShell script. Due to these difficulties, I didn’t want to go further with this approach, because I don’t want to having manage external libraries (and its nuget packages) within the pipeline for my PowerShell script, and I don’t want to deal with .Net versions either. I don’t want to be limited on what Azure Pipeline agent queues I can or cannot use because of the .Net versions – As far as I can see, the Newtonsoft.Json nuget package does not have a .Net Core version of the library. Even if I have got this script working in my pipeline, it will be too complicated and have too many limitations.

Luckily, The Microsoft.PowerShell.Utility module from PowerShell version 6 comes with a cmdlet Test-Json, which allows you to validate a JSON file against a schema file. This cmdlet is not available in Windows PowerShell version 5. I decided to go down this route, because the only dependency is that I have to use PowerShell Core, which is available in most the Azure Pipeline Microsoft-hosted agents.

I coded the script into a PowerShell module, so I can define module dependency (i.e. Pester, PowerShell version 6), and use Azure Artifacts to host it, so I can install the module from the Azure Artifacts feed to the hosted agents during the execution of the build pipeline.

I may cover how I use Azure Artifacts to host PowerShell modules and use them in CI/CD pipelines another time.

PowerShell Module: TestJSONSchema

As I mentioned earlier, this module requires PowerShell Core (version 6+), and Pester. You will need to install PowerShell Core and Pester module in PowerShell Core. The instruction is provided in the README.md file in the GitHub repo.

The module provides a function called Test-JsonSchema, it Pester tests one or more files against a specified schema file:


You can use Get-Help to read the detailed help file of this function.

Important Note: This module is NOT designed to validate Azure Resource Manager (ARM) templates. The ARM templates support features that are not natively defined the the JSON specifications (such as single-line and multi-line comments). If your goal is to pester test your ARM templates, the ARM product group in Microsoft has provided an awesome solution, you can find it from the Azure Quickstart Templates repo: https://github.com/Azure/azure-quickstart-templates/tree/master/test/template-tests

Leave a comment