For some reason there is a growing number of applications, frameworks and tools in my professional life that are configured via large JSON structures. Examples include Splunk SOAR Connectors, the Splunk UCC Generator or the all-new Splunk dashboard definitions. Editing these files by hand is error-prone and a little validation goes a long way to improve confidence in edits.
The easiest way to achieve that is to supply your editing environment, VS Code in my case, a JSON Schema. JSON Schema is to JSON what XSD is for XML - it defines an expected structure for the JSON data and enables programmatic validation.
There is a couple of ways how to get your JSON Schema used by VS Code o!
via extension
Under the covers, VS Code Extensions can supply schemas for files they want to provide editing assistance for. The contributes.jsonValidation
contribution point allows extension developers to add schema definitions and patterns on which to activate them.
{
"contributes": {
"jsonValidation": [
{
"fileMatch": ".jshintrc",
"url": "https://json.schemastore.org/jshintrc"
}
]
}
}
via user settings
But what if there would be an easier way, without developing a dedicated extension? Well, we’re all in luck because users can configure schemas in their User Settings under the json.schemas
key.
"json.schemas": [
{
"fileMatch": [
"/.jshintrc"
],
"url": "https://json.schemastore.org/jshintrc"
}
]
That’s cool - but what if there is no easy say to match your file name - Let’s say the file is named <your-appname>.json
. Unless you want to enable the schema for all JSON files (*.json
) you’re out of luck.
via $schema
But wait, there is one more thing we can do! There is a VS Code specific feature - the $schema
key.
If your JSON allows, you can add a $schema
pointing to your JSON Schema. It can be located on a given URL, or even within a local file:
{
"$schema": "./schemas/employee.json"
"id": 1,
"name": Daniel
}
Now, you’ll get autocompletions based on the supplied schema within that JSON file - great success!
Specifying the schema as part of the user settings is more convenient in my opinion - but $schema
is a day saver in cases where you can’t pattern match on the name of the file.