Variable Key Names for objects

1,858 views
Skip to first unread message

jrconlin

unread,
Sep 3, 2009, 4:45:54 PM9/3/09
to JSON Schema
Hi,

I don't believe this has been discussed yet, so my apologies if it
has.

I'm creating a schema for a fairly complex data structure, and for the
most part, things seem to fit fairly well. The only problem I'm
hitting is where I have an object who's keys may be variable, but
who's content is consistent.

For example, let's take the following code snippet:

{"catalog_title":
{"languages_and_audio":
{"French": [
"Dolby Digital 5.1"
"Dolby Digital 5.1"
],
"Spanish": [
"Dolby Digital 5.1"
"Dolby Digital 5.1"
],
"English": [
"Dolby Digital 5.1"
"Dolby True HD"
"Dolby Digital 5.1"
],
}
}

Granted, if I wanted to simplify this from a schema constructor point
of view, I'd create these as an array of objects where each object
specified the title name. That's great, but one of my other goals is I
wanted to make the JSON structure as small as possible meaning that
you could use a call like

for (audio in movie.catalog_title.languages_and_audio.English)
{ display(audio); }

That aside, the question I have is how do I specify a wildcard title
value in an object's properties section? I don't have a list of all
available languages (although I know that each will be unique), and I
know that for each language there will be an array of strings.

Is there a means to specify that sort of thing (say with a "*" title)
within the schema or am I out of luck?

Marak Squires

unread,
Sep 3, 2009, 4:57:58 PM9/3/09
to json-...@googlegroups.com
I don't really understand what you are asking....

Are you familiar with how to use for in loops over JSON objects?
Are you looking for a way to query json objects? If so try jsonpath or jsonquery.

Why would you need to use a wild card in the json definition? Can you elaborate more on your question? I think you might have a basic misunderstanding of JSON.

-Marak

Kris Zyp

unread,
Sep 3, 2009, 5:11:14 PM9/3/09
to json-...@googlegroups.com
Use the additionalProperties attribute to cover all the properties that
can have any name:
...
"properties":{
"languages_and_audio" :{
"additionalProperties:{
"type":"array",
"items":{"type":"string"}
}
}
..

Marak Squires

unread,
Sep 3, 2009, 5:34:44 PM9/3/09
to json-...@googlegroups.com
Ohh man, I didn't realize this was the json-schema mailing list. I just joined yesterday and thought this was a regular javascript mailing group!

Sorry, my mistake!

>.<

Roland Bouman

unread,
Sep 12, 2009, 3:48:46 PM9/12/09
to JSON Schema
Hi!

I am hitting the original problem posed by jrconlin too. But I don't
see how additionalProperties can help in my case.
My structure is such that I have an instance object that has a number
of properties that are maps, and I implement those as object
literals.
For one map, the items all should have the same type, but I don't know
the keys of the items in advance.

Now, like jcronlin suggested, I could define these maps as arrays, but
I just moved from arrays to maps because my application references the
items in the maps by name.
By using an object literal using the names as keys seems like a
natural way to make this as easy as possible.

Any ideas or suggestions are greatly appreciated.

kind regards,

Roland Bouman

Seth Wessitsh

unread,
Sep 12, 2009, 4:26:45 PM9/12/09
to json-...@googlegroups.com
Not sure I'm understanding your structure properly, but if I am and the property names of the instance are known then I would think this would work out:

{
   type: "object",
   properties: {
      prop1: {type: "object", additionalProperties: <mapValueType>}
      ...
   }
}


If they're unknown then this should do:

{
   type: "object",
   additionalProperties: {type: "object", additionalProperties: <mapValueType>}
}
 
Seth C. Wessitsh
se...@wessitsh.com

Roland Bouman

unread,
Sep 12, 2009, 4:39:09 PM9/12/09
to json-...@googlegroups.com
Seth, thanks for the reply!

Actually - I think I understand now! thanks!
--
Roland Bouman
http://rpbouman.blogspot.com/

Author of "Pentaho Solutions: Business Intelligence and Data
Warehousing with Pentaho and MySQL",
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470484322.html
Reply all
Reply to author
Forward
0 new messages