Recursive JSON schema

3,223 views
Skip to first unread message

Immad Naseer

unread,
Jun 9, 2010, 12:10:09 AM6/9/10
to json-...@googlegroups.com
Hi all,

I am having a little difficulty specifying a recursive JSON schema.

As a simple example, consider a folder object which can contain a list of file objects or nested folder objects. A sample object might look like:

var myFolder = {
    folder: [
        { file: "file.txt" },
        { file: "file2.txt" },
        { folder: [
            { file: "anotherFile.txt" },
            { folder: [] }
          ]
        }
    ]
}

Here is one attempt at defining the schema which did not work:

var fileSchema = {
    "properties": {
        "file": {
            "type": "string"
        }

}

var folderSchema = {
    "id": "folderSchema",
    "properties": {
        "folder": {
            "type": "array",
            "items": {
                "type": [ fileSchema, {"$ref": "folderSchema"} ]
            }
        }
    }
}

I am not deeply familiar w/ the $ref syntax so I might have made some simple mistake above. 

I will appreciate any feedback.

Cheers,
Immad

Ganesh and Sashi Prasad

unread,
Jun 9, 2010, 3:36:31 PM6/9/10
to json-...@googlegroups.com
Immad,

I'm not sure how to specify this in JSON Schema, but the UML way of specifying this kind of recursive relationship goes something like this (set the font to a fixed-width style like Courier if the diagram gets mangled):

                 |-----------|
                 |    Node   |<---------------------|
                 |-----------|                      |
                      / \                           |
                       -                            |
                       |                            |
                       |                            |
      -----------------------------------           |
      |                                 |           |
      |                                 |           |
|-----------|                     |-----------|     |
|    File   |                     |   Folder  |<>---|
|-----------|                     |-----------|

i.e, File and Folder are subclasses of Node. A Folder contains Nodes.

You may find it easier to model your structure in JSON Schema if you introduce the superclass "Node".

Regards,
Ganesh

--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To post to this group, send email to json-...@googlegroups.com.
To unsubscribe from this group, send email to json-schema...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/json-schema?hl=en.

Gary Court

unread,
Jun 9, 2010, 10:21:52 PM6/9/10
to JSON Schema
Using the JSON Schema defined in the revision 2 RFC, you would define
it as:

{
type : "object",
properties : {
folder : {
type : "array",
items : {
type : [
{
type : "object",
properties : {
file : {type : "string"}
},
additionalProperties : false
},
{$ref : "#"}
]
}
}
},
additionalProperties : false

Immad Naseer

unread,
Jun 9, 2010, 11:06:04 PM6/9/10
to json-...@googlegroups.com
Thanks for the feedback Gary and Ganesh.

A related question: does the javascript json schema validator (at http://code.google.com/p/jsonschema/) implement the spec? 

I tried to validate several examples using this schema through jsonschema-b4.js but some of them didn't work; { folder: [{ dummy: "file1.txt" } ] } is one example.

Are there any other validators I can take a look at which support the spec?

Thanks,
Immad


Dean Landolt

unread,
Jun 11, 2010, 1:22:20 PM6/11/10
to json-...@googlegroups.com
On Wed, Jun 9, 2010 at 11:06 PM, Immad Naseer <immad....@gmail.com> wrote:
Thanks for the feedback Gary and Ganesh.

A related question: does the javascript json schema validator (at http://code.google.com/p/jsonschema/) implement the spec? 

Gary Court

unread,
Jun 11, 2010, 10:40:35 PM6/11/10
to JSON Schema
There are a few validators out there that implement various portions
of the spec, but I don't believe there are any that implements the
complete revision 2 RFC... yet.

I say yet as I am working on one myself right now that should be fully
compliant with the current spec, and hope to release it in a couple
weeks. Keep an eye on the forum for my announcement.

On Jun 9, 9:06 pm, Immad Naseer <immad.nas...@gmail.com> wrote:
> Thanks for the feedback Gary and Ganesh.
>
> A related question: does the javascript json schema validator (athttp://code.google.com/p/jsonschema/) implement the spec?
> > json-schema...@googlegroups.com<json-schema%2Bunsubscribe@googlegr oups.com>
> > .

neonstalwart

unread,
Jun 12, 2010, 10:37:38 PM6/12/10
to JSON Schema
i'm not sure about the validator you're using but if you were using
gary's schema then i'm fairly sure your example won't validate. the
item which is a child of the 'folder' property is an object with a
property called 'dummy'. the schema says that children of 'folder'
can only have a 'file' property. i believe if you changed 'dummy' to
'file' then it would be valid against gary's schema.

ben...

On Jun 9, 11:06 pm, Immad Naseer <immad.nas...@gmail.com> wrote:
> Thanks for the feedback Gary and Ganesh.
>
> A related question: does the javascript json schema validator (athttp://code.google.com/p/jsonschema/) implement the spec?
> > json-schema...@googlegroups.com<json-schema%2Bunsubscribe@googlegr oups.com>
> > .

Immad Naseer

unread,
Jun 12, 2010, 3:01:30 AM6/12/10
to json-...@googlegroups.com
Thanks for the suggestion Dean -- I already had checked that one out before.

Awesome to hear that Gary; I will definitely be looking forward to it.

Thanks!
Immad

To unsubscribe from this group, send email to json-schema...@googlegroups.com.

Immad Naseer

unread,
Jun 13, 2010, 12:22:04 AM6/13/10
to json-...@googlegroups.com
I was validating through the jsonschema-b4.js available at http://code.google.com/p/jsonschema/

The validator confirms the presence of a root folder element but the {$ref : "#"} part confuses it and it basically does no checks on the contents of the folder; so something like { folder: [ 123 ] } passes as well. Removing {$ref : "#"} makes it validate the contents of the folder alright but you lose the ability to express nested folders.

Immad

To unsubscribe from this group, send email to json-schema...@googlegroups.com.

Matthew W

unread,
Jun 16, 2010, 1:05:47 PM6/16/10
to JSON Schema
On Jun 12, 3:40 am, Gary Court <gary.co...@gmail.com> wrote:
> There are a few validators out there that implement various portions
> of the spec, but I don't believe there are any that implements the
> complete revision 2 RFC... yet.
>
> I say yet as I am working on one myself right now that should be fully
> compliant with the current spec, and hope to release it in a couple
> weeks. Keep an eye on the forum for my announcement.

That's good news - although this seems the wrong way round to me.
Surely at least one solid, well-tested implementation of the concepts
introduced by this v2 spec should be required before trying to
standardise it via the RFC process?

Trying to specify things without actually having tried to implement
them is rather a dangerous thing IMO, and worries me especially when
it comes to relatively deep topics like the foundations of json-ref,
handling of object graphs, the bootstrapping of a validator based on
metacircular metaschemas, and interactions between all these things.

Are you attempting a full implementation of some of the (at present
IMO underspecified, but implicitly relied-upon) json-ref stuff? are
you basing it on the hyper-schema 'full' and 'self' relations? Has
the implementation been able to bootstrap itself successfully from the
metaschemas?

I'd be interested to hear how the implementation effort goes anyway,
and hope any lessons learned or awkwardnesses revealed, manage to feed
back into the specification process.

-Matt

Tatu Saloranta

unread,
Jun 16, 2010, 8:45:52 PM6/16/10
to json-...@googlegroups.com
On Wed, Jun 16, 2010 at 10:05 AM, Matthew W <matthew...@gmail.com> wrote:
> On Jun 12, 3:40 am, Gary Court <gary.co...@gmail.com> wrote:
...

>> I say yet as I am working on one myself right now that should be fully
>> compliant with the current spec, and hope to release it in a couple
>> weeks. Keep an eye on the forum for my announcement.
>
> That's good news - although this seems the wrong way round to me.
> Surely at least one solid, well-tested implementation of the concepts
> introduced by this v2 spec should be required before trying to
> standardise it via the RFC process?
>
> Trying to specify things without actually having tried to implement
> them is rather a dangerous thing IMO, and worries me especially when
...

+1 for this. There are so many examples of pre-mature standadrization
("pre-mature standardization is bigger source of evil than pre-mature
optimization" - T.S) that at least one compliant implementation should
be a requirement for finalizing specifications (sometimes spec groups
cheat a bit, too, having ref. impl. that are incomplete; or lack
actual compatibility test to verify).
It sounds like that would be roughly the plan here too?

-+ Tatu +-

Gary Court

unread,
Jun 16, 2010, 10:56:59 PM6/16/10
to JSON Schema
> That's good news - although this seems the wrong way round to me.
> Surely at least one solid, well-tested implementation of the concepts
> introduced by this v2 spec should be required before trying to
> standardise it via the RFC process?

Yes, I agree. Although currently the spec is still a draft, which (as
I believe) leaves it open for changes and discussion.

> Trying to specify things without actually having tried to implement
> them is rather a dangerous thing IMO, and worries me especially when
> it comes to relatively deep topics like the foundations of json-ref,
> handling of object graphs, the bootstrapping of a validator based on
> metacircular metaschemas, and interactions between all these things.
>
> Are you attempting a full implementation of some of the (at present
> IMO underspecified, but implicitly relied-upon) json-ref stuff? are
> you basing it on the hyper-schema 'full' and 'self' relations? Has
> the implementation been able to bootstrap itself successfully from the
> metaschemas?

I'm attempt a full implementation where possible based off the spec
definition of how hyper-schema is supposed to work. The schemas on
http://json-schema.org are horribly outdated/broken so I've rewritten
them based off the latest spec. Bootstrapping off a schema almost
works, and I am currently able to self-validate my version of the JSON
Schema schema.

> I'd be interested to hear how the implementation effort goes anyway,
> and hope any lessons learned or awkwardnesses revealed, manage to feed
> back into the specification process.

Yes, I've been feeding my discoveries back into the forum as I find
them. So far I have found a couple issues with the rev 2 spec that I
have brought up for discussion here. (For example, see my post on why
the "optional" attribute is broken)

I'm doing this in my spare time, so it may take awhile to get it
finished to the point where I'd be comfortable releasing it. I'll let
you know how it goes. Thanks for the support!
Reply all
Reply to author
Forward
0 new messages