5

I'm noob to json. While defining the format of my RESTful API's result (viz JSON), I felt it would be easier to document it as my own JSON schema. While writing one I had few questions:

  1. In my result JSON, how do I specify the URI to teh schema it confirms to? --edit-- is it using $schema attribute?
  2. Are there any conventions/guidelines for JSON schema versioning? Are there some attributes that I should/can define inside my schema as attributes? I see JSON schema itself has no version defined except in it's URI specified as value of key $schema.
  3. Can I break down my one BIG JSON schema into multiple smaller ones and include one in another? Like #include in C++, then refer to multiple schemas in the JSON I sent to user as result.
  4. Can I define a custom value for key "type"? E.g. I would like to reuse the definition of "date" like this:

[ignore this line, it's to get the formatting working for following json..]

{
    "date":{
        "type":"object",
        "properties":{
            "month":{
                "type":"integer",
                "minimum":1,
                "maximum":12
            },
            "year":{
                "type":"integer",
                "minimum":0
            }
        }
    },
    "personInfo":{
        "type":"object",
        "properties":{
            "name":{
                "type":"string"
            },
            "dateOfBirth":{
                "type":"date"
            }
        }
    },
    "student":{
        "type":"object",
        "properties":{
            "id":{
                "type":"personInfo"
            },
            "pass_out_year":{
                "type":"date"
            }
        }
    }
}

instead of providing properties of "date" in multiple places like this:

{
    "personInfo":{
        "type":"object",
        "properties":{
            "name":{
                "type":"string"
            },
            "dateOfBirth":{
                "type":"object",
                "properties":{
                    "month":{
                        "type":"integer",
                        "minimum":1,
                        "maximum":12
                    },
                    "year":{
                        "type":"integer",
                        "minimum":0
                    }
                }
            }
        }
    },
    "student":{
        "type":"object",
        "properties":{
            "id":{
                "type":"personInfo"
            },
            "pass_out_year":{
                "type":"object",
                "properties":{
                    "month":{
                        "type":"integer",
                        "minimum":1,
                        "maximum":12
                    },
                    "year":{
                        "type":"integer",
                        "minimum":0
                    }
                }
            }
        }
    }
}

according to 5.1 type in the spec, it's not possible, but it seems like such a basic usecase!

Community
  • 1
  • 1
Kashyap
  • 15,354
  • 13
  • 64
  • 103

4 Answers4

4
  1. As you correctly figured out, $schema can be used to specify the schema it conforms to.
  2. I actually found this topic while googling for JSON Schema versioning, and the idea of using the URI for versioning sounds logical.
  3. You can use $ref to link to another schema that is then pulled in.
  4. Again, you could use $ref and JSON Pointer to import definitions from other schemas.

You can always test things out by validating your schema to see if you've made any mistakes.

Community
  • 1
  • 1
Miha Hribar
  • 5,776
  • 3
  • 26
  • 24
3

At the time of this writing, the current version of the JSON Schema specification is draft-v4, in which the format date-time for string instances is clearly specified and is very useful in practice.

There is no simpler format date defined so far, but you can easily define your object's property as type string and then apply a format pattern validation (ECMA 262 regex dialect) on top of it.

For example:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "title": "Example Schema"
    "description": "This schema contains only a birth date property"
    "type": "object",
    "required": ["birth_date"],
    "properties": {
        "birth_date": {
            "type": "string",
            "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
        }
    }
}
jose.angel.jimenez
  • 2,127
  • 23
  • 17
  • But what if he has a birth_date and death_date and join_date and last_modified_date and etc... what then, duplicate the pattern everywhere? – Joshua Cheek May 27 '17 at 01:32
2

Why don't you just use the "format" : "date" as per #5.23 in JSON Schema Draft 03?

Plus your definition of birth date doesn't contain date which seems to be an error.

Community
  • 1
  • 1
Artem Oboturov
  • 4,344
  • 2
  • 30
  • 48
  • Thanks. The question was more to understand how to define custom structs rather than how to define 'date', which was meant as an example only. – Kashyap Sep 17 '13 at 18:48
  • @artemoboturov The link to #5.23 is valid for JSON Schema Draft 3, but in latest "stable draft" 04 it is not present. [JSON Schema Validation document in section 7.3.1](http://json-schema.org/latest/json-schema-validation.html#anchor108) declares "format" with value "date-time". Unfortunately there is no "date". – Jan Vlcinsky Jun 30 '14 at 11:27
1

The spec seems to suggest you could:

Other type values MAY be used for custom purposes,...

It then goes on to discuss what validators of the minimal implementation might do.

To my mind, what you want to do seems OK. It might help your schema's clarity to keep the type reference and type definition in the same file.

I think your file includes Q should be handled outside of json, e.g. have a dev step that generates the complete json from a script/template (e.g. erb or some such) merging your subfiles. To my mind your service should always deliver the full json required to fully interact with that service. If this gets unmanageable from the clients perspective, it may be a signal to refactor and introduce another service.

Hedgehog
  • 5,487
  • 4
  • 36
  • 43
  • Seems like too much work, so I won't do it. But yes, a feasible option, especially in today's tech-env I'm sure to find tools to do this in any language/env. Thanks. – Kashyap Sep 17 '13 at 18:50
  • In v4 of JSON schema this seems to not be allowed anymore: http://json-schema.org/latest/json-schema-validation.html#anchor79 – Mitar May 30 '15 at 03:32