Mark T
Mark T

Reputation: 89

How should you reference the JSON schema that a JSON object conforms to?

There was another question similar to the one I am asking here (Can you specify the schema URI on a JSON document that conforms to a JSON schema?) that was tagged as a duplicate of (How to reference schema of json which is top level array), but I have nuanced variation of the question.

While it appears that there isn't anything in the JSON schema definition (https://json-schema.org/), is there a best practice that people follow in regards to indicating within a JSON object/document which JSON schema it conforms to (or is supposed to conform to)?

Would it be wrong to reference the schema using the $schema tag within a JSON object/document? It seems like referencing the schema it conforms to would also be a be a good way to "version" JSON objects/documents.

Upvotes: 8

Views: 2025

Answers (1)

tom redfern
tom redfern

Reputation: 31780

I very much doubt there is an "acceptable" answer to this question.

There's nothing to stop you from using the $schema attribute as part of your own personal convention.

There is some real-world precedent to this. Some Azure quickstart templates use $schema to reference the JSON Schema they adhere to eg: https://github.com/Azure/azure-quickstart-templates/blob/master/101-azure-bastion/azuredeploy.parameters.json (Thanks to Mark T for his comment)

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
      "bastion-host-name": {
        "value": "GEN-UNIQUE-8"
    },
      "location": {
        "value": "southcentralus"
    }
  }
}

However, with a lack of official guidance we should perhaps just accept that the concept of schema-instance (as taken from XSD) is not a good fit for a lightweight definition language such as jsonschema.

I would be interested to understand your use-case where linking JSON instances explicitly with their schemas is a requirement.

Upvotes: 4

Related Questions