How to specify object names

33 views
Skip to first unread message

Andy

unread,
Sep 16, 2010, 7:54:08 AM9/16/10
to JSON Schema
Hi,
I'd like to define a JSON schema for the following JSON example:

{
"links": [
"self": {
"href": "some-uri"
},
"parent": {
"href": "some-other-uri"
}
]
}

The names "self" and "parent" are examples and can be chosen by the
JSON instance. The names "links" and "href" are mandated by the JSON
schema.

What I am missing in the IETF draft for the JSON schema is a way to
state that the items in the "links" have to have a name, and a way to
provide a description for that name.

To illustrate what I have in mind, here is a JSON schema for the above
example that uses an assumed "name" property which would describe the
names of the JSON objects:

{
"description": "A set of links.",
"name": "links", # an assumed name property, defining that the
object is named
# and that the name must be "links".
"type": "array",
"items": {
"description": "Each array item is a JSON object representing a
link.",
"name": { # an assumed name property, defining that the object is
named
# but with no required value, and providing a type
and description.
"description": "The name of the JSON object is the name of the
link.",
"type": "string" # I guess anything else but string would not be
allowed as a type
},
"type": "object",
"properties": {
"href": {
"description": "The value of the href property is a URI that
is the target of the link.",
"type": "string"
}
}
}
}

I know that a "name" property is not part of the current JSON schema
draft (02) on IETF, I just inserted it to illustrate what I have in
mind.

I would like to understand:
1. Can I state something like this when usin gthe current JSON schema
draft 02 ?
2. If not, are there ideas on how to add such a capability to JSON
schema ?

Thanks
Andy

Dean Landolt

unread,
Sep 16, 2010, 11:01:53 AM9/16/10
to json-...@googlegroups.com
On Thu, Sep 16, 2010 at 7:54 AM, Andy <andreas...@gmx.de> wrote:
Hi,
I'd like to define a JSON schema for the following JSON example:

{
 "links": [
   "self": {
     "href": "some-uri"
   },
   "parent": {
     "href": "some-other-uri"
   }
 ]
}

This is not valid JSON -- look a little closer at the contents of your list...

This suggests you're missing an object wrapper, and thus your prop names are missing a key. The key you're looking for is "rel":

{
 "links": [
   {
     "rel": self",
     "href": "some-uri"
   },
   {
     "rel": "parent",
     "href": "some-other-uri"
   }
 ]
}


Now you've got valid JSON (and valid JSON Schema Links).

Andy

unread,
Sep 17, 2010, 4:55:19 AM9/17/10
to JSON Schema
Dean,
thanks for the clarification. I was trying to apply the idea of
associative arrays to the standard link definition in the draft JSON
schema RFC, in order to improve direct access to a link, by name (see
http://ekrantz.com/index.php/2007/06/06/json-and-associative-arrays.html).
While applying that idea, I used [] where these examples used {},
probably by blindly following my expectation that an associative
*array* would use []. Sorry for that.

All,
Direct access by name is an important criteria for what we are doing,
and the standard link definition in the draft JSON schema RFC misses
that point, unfortunately. We also need this for other cases of direct
access, I was just using the link case as an example.

Let me know if you think that direct access to a specific link by its
link name is something that can be done efficiently and with minimal
coding (i.e. without a loop) from languages like Python or Javascript
by using properties of array items, as in the standard link definition
when using the "rel" property as the link name. My assumption was that
this is not possible, and that the associative array approach
described in the referenced article is what enables that.

So here is my next attempt, with some more questions:

Schema:

{
"name": "LinksSchema",
"description": "A set of links.",
"type": "object",
"properties": {
"<link-name>": {
"description": "A link. <link-name> is the name of the link.
There is one <link-name> property for each link in the set.",
"type": "object",
"properties": {
"href": {
"description": "A URI that is the target of the link.",
"type": "string"
}
}
}
}
}

Example property whose value is described by that schema:

"links": {
"self": {
"href": "some-uri"
},
"parent": {
"href": "some-other-uri"
}
}

Again, I am aware that the standard link definition is different, and
I have other use cases that lead to the same questions.

My questions are:

- Is there a better way to describe the name of the link (which is
unknown at the time the schema is defined) than by inventing a
template syntax such as "<link-name>" ?

- I'd like to use this schema in multiple other schemas without
repeating it. Is there a way a schema defining an object can be named
and referenced for defining an object type in another schemas ? More
or less like a type name in W3C XML Schema?

- I found the draft RFC uses a "name" property in its first example
(the first occurence of "name" directly after the top-level left
brace) that sounds like a schema name, but the draft RFC does not
explain that property anywhere. The schema shown above used that as
well. The schema and hyper-schema examples on json-schema.org use "id"
and "$schema" properties that seem to have an identifying role, but
again these properties are not described in the draft RFC. Does anyone
know the intention of the IETF group owning the draft RFC, for these
three properties (name, id, $schema) or for the general problem of
identifying and referencing a schema ?

Andy
Reply all
Reply to author
Forward
0 new messages