Implementing an object within an object in the JSON schema

21 views
Skip to first unread message

z.gal...@gmail.com

unread,
Mar 31, 2014, 12:26:40 PM3/31/14
to json-...@googlegroups.com
I am using this JSON schema example (http://json-schema.org/example1.html) to learn about the JSON schema. In formulating my own schema, I have come across a situation I don't fully understand how to implement. Suppose I am trying to describe an event held over multiple days. Each date has a different start and date time.

Does it make sense to define the schema like this 

"eventDates" :{
"eventStartDateTime":{
"type":"string"
},
"eventEndDateTime":{
"type":"string"
}
         }

and an individual event like this?

“eventDates”: {
{“eventStartDateTime”:{“2014-04-12T20:00+00:00”},
“eventEndDateTime”:{“2014-04-12T23:00+00:00”}}, 
{“eventStartDateTime”:{“2014-07-12T16:00+00:00”},
“eventEndDateTime”:{“2014-07-12T19:00+00:00”}}
}

Francis Galiegue

unread,
Mar 31, 2014, 12:36:11 PM3/31/14
to json-...@googlegroups.com
That won't work since this is not valid JSON to start with! What you
should do is use an array:

{
"eventDates": [
{ "eventStartDateTime": "2014-04-12T20:00+00:00",
"eventEndDateTime": "2014-04-12T23:00+00:00" },
// etc
]
}

Then your schema would be:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"event": {
"type": "object",
"required": [ "eventStartDateTime", "eventEndDateTime" ],
"additionalProperties": false,
"properties": {
"eventStartDateTime": { "type": "string", "format":
"date-time" },
"eventStartDateTime": { "type": "string", "format":
"date-time" }
}
}
},
"type": "object",
"required": [ "eventDates" ],
"additionalProperties": false,
"properties": {
"eventDates": { "type": "array", "items": { "$ref":
"#/definitions/event"}
}
}


--
Francis Galiegue, fgal...@gmail.com
JSON Schema in Java: http://json-schema-validator.herokuapp.com

z.gal...@gmail.com

unread,
Mar 31, 2014, 12:50:51 PM3/31/14
to json-...@googlegroups.com, z.gal...@gmail.com
Thanks for the explanation. Using your schema, would an event look like this:

“event”:{

          “eventDates”:{

               [“eventDateStartTime”:{“2014-04-12T20:00+00:00”},

               “eventEndDateTime”:{“2014-04-12T23:00+00:00”}], 

               [“eventStartDateTime”:{“2014-07-12T16:00+00:00”},

              “eventEndDateTime”:{“2014-07-12T19:00+00:00”}]

Francis Galiegue

unread,
Mar 31, 2014, 1:03:50 PM3/31/14
to json-...@googlegroups.com, z.gal...@gmail.com
On Mon, Mar 31, 2014 at 6:50 PM, <z.gal...@gmail.com> wrote:
> Thanks for the explanation. Using your schema, would an event look like
> this:
>

No... Again, this is not valid JSON.

Here is a schema and a matching value:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"eventDate": {
"type": "object",
"required": [ "eventStartDateTime", "eventEndDateTime" ],
"additionalProperties": false,
"properties": {
"eventStartDateTime": {
"type": "string",
"format": "date-time"
},
"eventStartEndTime": {
"type": "string",
"format": "date-time"
}
}
},
"eventDates": {
"type": "object",
"required": [ "eventDates" ],
"additionalProperties": false,
"properties": {
"eventDates": {
"type": "array",
"items": {
"$ref": "#/definitions/eventDate"
}
}
}
}
},
"type": "object",
"required": [ "event" ],
"additionalProperties": false,
"properties": {
"event": {
"type": "object",
"items": {
"$ref": "#/definitions/eventDates"
}
}
}
}

and the value:

{
"event": {
"eventDates": [
{
"eventDateStartTime": "2014-04-12T20:00+00:00",
"eventEndDateTime": "2014-04-12T23:00+00:00"
},
{
"eventStartDateTime": "2014-07-12T16:00+00:00",
"eventEndDateTime": "2014-07-12T19:00+00:00"
}
]
}
}

Hope this helps,

z.gal...@gmail.com

unread,
Mar 31, 2014, 1:05:09 PM3/31/14
to json-...@googlegroups.com, z.gal...@gmail.com
It does. Thanks again.


On Monday, March 31, 2014 12:26:40 PM UTC-4, z.gal...@gmail.com wrote:

Francis Galiegue

unread,
Mar 31, 2014, 1:05:48 PM3/31/14
to json-...@googlegroups.com, z.gal...@gmail.com
Oops... Not the good schema. Here it is:
"properties": {
"eventDates": {
"$ref": "#/definitions/eventDates"
Reply all
Reply to author
Forward
0 new messages