How do I determine which JSON Schema to use to validate a JSON instance?

145 views
Skip to first unread message

Roger Costello

unread,
Aug 12, 2015, 12:13:03 PM8/12/15
to JSON Schema
Hi Folks

In XML one can often determine the appropriate XML Schema to use to validate an XML instance by examining the namespace used in the XML instance.

JSON does not use namespaces, so how would one determine the appropriate JSON Schema to use to validate a JSON instance?

I have many different JSON Schemas and I receive JSON instances from many different sources. I need an automated way to determine which JSON Schema to use to validate each JSON instance.

How are you accomplishing this?

/Roger

Jason Desrosiers

unread,
Aug 13, 2015, 12:28:31 AM8/13/15
to JSON Schema
http://json-schema.org/latest/json-schema-core.html#anchor33

Checkout this section of the documentation.  It describes how to use HTTP headers to correlate a schema with a JSON document.

xmlbuddy

unread,
Aug 13, 2015, 7:27:24 AM8/13/15
to JSON Schema
This works for JSON and JSON schema and using HTTP.

AFAIK there is no standard mechanism to reference a JSON schema from a JSON instance in a local (file system) environment. In my editor I save the path to the JSON schema as an external setting (in a database). However, if there is a more common method available I would love to learn about it.

Is this actually a topic for the next draft of JSON schema?

Regards
Clemens

Jason Desrosiers

unread,
Aug 16, 2015, 11:40:11 PM8/16/15
to JSON Schema
No, there is no standard mechanism for self-descriptive JSON.  


However, I ran across this proposal not long ago.  I like the concept of self-descriptive JSON, but I am not a fan of this particular proposal.  First, the proposal defines some new JSON Schema keywords to make the schema self descriptive.  This is unnecessary.  JSON Schema is already fully self descriptive without any of these additions.  Next, the proposal describes a JSON structure with a mechanism for specifying the schema that describes the data.  I think this structure is quite awkward.  But to be fair, the simplicity of the JSON format makes this kind of thing difficult to express.

If I needed to associate a JSON document outside the context of HTTP, I would do something like what Clemens describes and store it as a tuple of JSON document and schema URL.

peff...@gmail.com

unread,
Oct 26, 2015, 4:41:39 PM10/26/15
to JSON Schema
Some tools (for example, Microsoft Visual Studio and Visual Studio Code) will interpret a $schema property in the JSON file as the pointer to the corresponding schema and will use it for interactive validation. See

https://github.com/Azure/azure-quickstart-templates/blob/master/100-starter-template-with-validation/azuredeploy.json for an example.


iouri

Geraint

unread,
Nov 3, 2015, 8:03:56 AM11/3/15
to JSON Schema, peff...@gmail.com
While the Visual Studio behaviour is non-standard, it is intuitive.  As heuristics go, this seems pretty sane to me.

We don't really have the authority to globally reserve a JSON property for our own use.  However, it would be entirely possible to describe the problem (particularly when using local-filesystem JSON documents) and explain how some existing editors behave, and leave implementors to decide whether they want to follow an existing heuristic.
Reply all
Reply to author
Forward
0 new messages