Recursive Schema

75 views
Skip to first unread message

Sebastian Kurfuerst

unread,
Nov 4, 2010, 6:42:01 AM11/4/10
to JSON Schema
Hey everybody,

I am looking for how a recursive schema, like a schema for a file
tree, can be defined. Imagine we have the following tree:

{
name: '/'
children: [
{
name: 'test',
children: [....]
}
]
}
So, basically I want to evaluate the following:
- make sure "name" is present everywhere
- if there are children, that should be an array.

So something like this:

{
type: 'object',
properties: {
name: {type: 'string'},
children: {type: 'array', optional: true, items: /*REFERENCE TO
"OBJECT" ABOVE */}
}
}

It'd be really great if somebody could give me a hint how to reference
the main object in this case.

Thanks a lot,
Sebastian
PS: I know this question has been asked, but I did not find any
answers yet which explained it nicely. Sorry!

Gary Court

unread,
Nov 13, 2010, 2:43:53 PM11/13/10
to json-...@googlegroups.com, sebastian...@gmail.com
You can create references using {"$ref" : "uri"} . All schemas have a
URI, and you can use relative URIs in your references. So, in your
example, your schema would look like:

{


type: 'object',
properties: {
name: {type: 'string'},

children: {type: 'array', optional: true, items: {'$ref': '#'}}
}
}

"#" is the fragment tag in a URI, which (on it's own) indicates the
root element of the URI.
You can also reference sub elements. For example, to reference
"children", you would use a relative URI of "#/properties/children".
You can also use named references by adding "id" attributes to your
schemas. So, for example, with the following schema:

{
id : '#root'
type: 'object',
properties: {
name: {id: '#name', type: 'string'},
children: {id: '#children', type: 'array', optional: true, items:
{'$ref' : '#root'}}
}
}

You can now reference children with the URI "#children", or name with "#name".

-Gary

> --
> 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.
>
>

Sebastian Kurfuerst

unread,
Nov 15, 2010, 3:40:11 AM11/15/10
to JSON Schema
Hello Gary,

that works like a charm as you described! Thanks a bunch for helping
me out :-)

Greets, and keep up the good work!
Sebastian
Reply all
Reply to author
Forward
0 new messages