Scheming with JSON
Today's topic is something I've had on my list for quite awhile. I thought I had already written about it because I found notes from 2023. However, as far as I can tell, I must have gotten distracted and never wrote the article. Let's fix that. This is an advanced topic that might not apply to you today. But who knows what you'll need to create tomorrow.
It is not uncommon to utilize JSON files in PowerShell scripting and toolmaking. You might use a JSON file to store configuration data. Or maybe you are using JSON as a storage mechanism. There is a JSON feature you might want to take advantage of: schemas. A schema is a blueprint for what a JSON file should look like. Using a schema ensures the JSON file is properly formatted to meet your requirements.
Let's take a look at creating and using a JSON schema.
Basic JSON
JSON schemas can complex very quickly. So let's start with a simple JSON file.
{
"Name": "Jeff",
"Version": 7.5,
"PSHome": "C:\\Program Files\\PowerShell\\7",
"Services": [
"WinRm",
"WinMgmt",
"WSearch"
],
"Comment": "This is demo data."
}
The schema will be defined as a separate JSON file. The needs the following properties:
- $schema The JSON Schema draft your schema adheres to. This is a schema keyword and it includes the $.
- $id A URI for the schema. This will be used in the JSON file to locate the schema. This is a schema keyword and it includes the $.
- title Provide a descriptive title for the schema. Treat this as metadata.
- description I recommend including a description that explains the purpose or background for the schema. Treat this as metadata.
- type A defining constraint that specifies what the JSON data defines. Most often, this will be
object
.
Here's how I might define the first part of the schema file.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "file:///c:/scripts/demo.schema.json",
"title": "Demonstration Schema",
"description": "A simple sample schema",
"type": "object",
The schema file itself is using schema reference that points to the most recent draft standard. The $id
property points to the path of my schema file.
Next, we need to define the properties of the JSON file. The properties are defined in a properties
object. Each property is defined as an object with its own set of properties. I recommend you set these properties at a minimum.
- description A description of the property.
- type The type of data the property should contain. This can be
string
,number
,integer
,object
,array
,boolean
andnull
. I've been treatingSystem.Double
values asnumber
. If in doubt,object
should be a good default.
There are many advanced things you can do when it comes to defining properties, but I want to keep it simple for now.
The last element you might want to include is a list of required properties. In my sample I want to require the Name
, Version
, and PSHome
properties. The required
property is an array of strings that contain the names of the required properties.
Here's my complete simple sample JSON schema file.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "file:///c:/scripts/demo.schema.json",
"title": "Demonstration JSON Schema",
"description": "A simple sample schema. Last updated 30 April 2025.",
"type": "object",
"properties": {
"Name": {
"description": "A user name",
"type": "string"
},
"Version": {
"description": "PowerShell version",
"type": "number"
},
"PSHome": {
"description": "PowerShell PSHome path",
"type": "string"
},
"Services": {
"description": "An array of Windows services",
"type": "array",
"items": {
"type": "string"
}
},
"Comment": {
"description": "An optional comment",
"type": "string"
}
},
"required": [
"Name",
"Version",
"PSHome",
"Services"
]
}