0

I have a JSON object:

{
    "name" : "car",
    "color" : "blue"
}

And a JSON Schema which is used to validate it (not specified here).

XML allows you to include a reference to an extern XML Schema Definition for validation in the XML document itself via "xsi:schemaLocation" (the namespace prefix might be different).

Is there an equivalent way to include a reference to a JSON Schema in a JSON object?

Sebastian Barth
  • 4,079
  • 7
  • 40
  • 59

1 Answers1

2

The link from data to a schema describing it is generally considered "metadata", so usually isn't in the data itself.

If you're working over HTTP, you can:

  • use a "profile=" parameter in the Content-Type header, or
  • use a "Link:" header, with rel="describedby" to link to the schema.

The second option is preferable.

If you're loading from a file, or something else, then it's not defined. Within your own application, you are perfectly free to define something like "$schema" to reference schemas. Although other tools wouldn't pick up on this automatically, it would be obvious to any other developers what you were doing.

cloudfeet
  • 12,156
  • 1
  • 56
  • 57
  • [Json-schema.org](http://json-schema.org/latest/json-schema-core.html#anchor33) says if a MIME type is present in the header it's RECOMMENDED to add a profile with URI to schema as value to the Content-Type header. Anything wrong with adding both, Link and profile? – Sebastian Barth Oct 07 '14 at 08:06
  • Not that I can see, although some tools might add the schema twice (or whatever), which could result in duplicate links etc. – cloudfeet Oct 07 '14 at 11:34
  • What is your intention to say the `Link:` way is preferable over the `Content-Type` way? – Sebastian Barth Oct 07 '14 at 15:47
  • It allows you to specify multiple schemas if you want (unlike Content-Type), it's consistent with the way you [use a schema to dynamically specify schemas from within data](http://stackoverflow.com/questions/19416873/how-to-tell-json-schema-validator-to-pick-schema-from-property-value/19425639#19425639), more transports/formats I've encountered include link-definitions than Content-Type, and I personally feel it's more elegant. :p – cloudfeet Oct 07 '14 at 15:55