Creating JSONSchema

277 views
Skip to first unread message

ashutosh raina

unread,
Nov 20, 2011, 1:02:47 PM11/20/11
to JSON Schema
I am looking to create JSONSchema for my application and ensure that a
validator is able to validate JSON Instances based on the schema. I
have a bunch of XSD , which have a bunchh of namespaces in them. The
XSD refer to each other for definitions. What would be the appropriate
way of converting them in to JSONSchema ?
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:i="http://niem.gov/niem/appinfo/2.0"
xmlns:s="http://niem.gov/niem/structures/2.0"
targetNamespace="http://niem.gov/niem/appinfo/2.0"
attributeFormDefault="qualified"
version="1">
How do i represent the above through JSONSchema ? what happens to the
following ?
<xsd:complexType name="AssociationType">
<xsd:annotation>
<xsd:appinfo>
<i:Base i:namespace="http://niem.gov/niem/structures/2.0"
i:name="Association"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="s:ComplexObjectType"/>
</xsd:complexContent>
</xsd:complexType>

How do i use $ref in this , what is the use of link ? how will these
get resolved in validation ? Even if there are no right answers for
this right now , but my guess is that this definitely worth looking
at .

Francis Galiegue

unread,
Nov 20, 2011, 2:17:17 PM11/20/11
to json-...@googlegroups.com

While I cannot answer everything because I don't know XSD (which, for
me, has been written by someone on drugs but that's another story), I
can give you the general principles.

A "fully qualified" schema, for lack of a better name, is always a
JSON object, and has the following elements always defined:

{
"$schema": "http://path.to/schema/version#",
"id": "http://location.of/this/schema#",
// etc -- more on this # later
}

where:

* "$schema" is an URI indicating that this schema is written with this
version; the URI points itself to a schema, which validates itself,
and the current schema;
* "id" is where your schema is located.

$ref is used like this:
{
"$ref": "http://location.of/a/schema#/path/into/that/schema/if/necessary"
}

This is called a JSON Reference. The URI is to be separated in two parts:

* "http://location.of/a/schema" --> location of the JSON document;
* "#/path/into/that/schema/if/necessary" --> the JSON Pointer against
which to resolve the JSON document to get the schema.

The JSON Pointer for the root of a JSON document is "#", hence the "#"
in the examples above means that the whole JSON document pointed by
the resource is the schema itself.

For your use case, you'd have the choice of:

* writing only one JSON document with all schemas in it, and address
them in $ref using "non empty" JSON Pointers:

----
{
"id": "http://where.the/document/is#",
"firstschema": { ...},
"secondschema": { ... }
}
----

and then address each schema by { "$ref":
"http://where.the.document/is#/firstschema" } and so on;

* writing one JSON document per schema, putting them at different
location, and using appropriate $refs with "empty" JSON Pointers.

Please wait for more authoritative answers, I have only understood the
mechanism a few weeks ago.

Hope this helps and doesn't confuse you,
--
Francis Galiegue, fgal...@gmail.com
"It seems obvious [...] that at least some 'business intelligence'
tools invest so much intelligence on the business side that they have
nothing left for generating SQL queries" (Stéphane Faroult, in "The
Art of SQL", ISBN 0-596-00894-5)

ashutosh raina

unread,
Nov 22, 2011, 6:33:37 AM11/22/11
to JSON Schema
Assuming i use the second option , how will i refer to a property from
another schema in the current schema?

How will i extend that property ? A very simple example would be
appreciated. Can we cover this case in the draft-specification and
hence have more clarity?

> Francis Galiegue, fgalie...@gmail.com

Francis Galiegue

unread,
Nov 22, 2011, 7:31:16 AM11/22/11
to json-...@googlegroups.com
On Tue, Nov 22, 2011 at 12:33, ashutosh raina
<ashutosh...@gmail.com> wrote:
> Assuming i use the second option , how will i refer to a property from
> another schema in the current schema?
>
> How will i extend that property ? A very simple example would be
> appreciated. Can we cover this case in the draft-specification and
> hence have more clarity?
>

I am not sure if I fully understand your question, but here is an example:

* one schema that you reference:

----
{
"id": "http://some.where/sub/schema#",
"type": "object",
"properties": {
"p1": {
"type": "integer",
"minimum": 12
}
}
}
----

* the main schema:


----
{
"id": "http://path.to/base/schema#",
"type": "array",
"items": {
"extends": {
"$ref": "http://some.where/sub/schema#/properties/p1"
},
"divisibleBy": 5
}
}
----

So, what does this say about the main schema:

* that the JSON document must be an array,
* that the items in the array must obey both:
** the schema found at JSON Pointer "#/properties/p1" in the JSON
document found at "http://some.where/sub/schema#" (the fact that the
JSON document is itself a schema does not matter at all here), which
says the property should be an integer with a minimum value of 12,
** the additional constraint that this same integer must be divisible by 5.

"extends" basically says that you must obey the referenced schemas
(because there _can_ be several schemas referenced in "extends", here
there is only one), plus the additional constraints.

Hope this helps,
--
Francis Galiegue, fgal...@gmail.com

Reply all
Reply to author
Forward
0 new messages