Map Definition

80 views
Skip to first unread message

Jacob

unread,
Oct 2, 2008, 12:11:28 PM10/2/08
to JSON Schema
How should a map/dictionary type be defined? Just an array of objects
with "key" and "value" properties? Is there an example already.

Side note: http://groups.google.com/group/json-schema/web/json-schema-examples
does not exist, but is linked to on the pages page. This would be
really valuable to see how people are defining schemas for common use
cases.

Jacob

Kris Zyp

unread,
Oct 2, 2008, 12:19:44 PM10/2/08
to json-...@googlegroups.com
Do you mean for a non-string-keyed map? Of course a JSON object is a
map/dict with string keys. For a map with other types of keys, I would
imagine that an array of objects with key and value property as you
suggested would be the most logical.

The referenced page contains examples that from an outdated version of the
proposal, and would not be helpful. Have you looked at these examples:
http://www.json-schema.org/shared.html
Kris

Jacob

unread,
Oct 2, 2008, 12:32:35 PM10/2/08
to JSON Schema
Lets say I want a string keyed map, but all the values of the same
object type. It is not clear to me how this is defined without the
array of objects.

For a completely open map, any old JSON object world work because
there would be no validation of key or value.

Jacob

Kris Zyp

unread,
Oct 2, 2008, 12:39:56 PM10/2/08
to json-...@googlegroups.com
I see what you are looking for, this is what the "additionalProperties"
attribute is for:
schema:
{
name:"String to number map",
additionalProperties:{type:"number"}
}
valid instance (all the properties must have a value that is a number):
{
"one":1,
"two":2,
"three":3
}

The additionalProperties attribute can take any schema, so you could have an
object definition in there as well.

Reply all
Reply to author
Forward
0 new messages