Publishing JSON Schema Documentaton with Docson

Mar 09, 2016

For systems that have JSON data that’s published as part of an API, there are tools such as Swagger / OpenAPI and RAML that can be used to define REST endpoints and the data types for requests and responses. However, for cases where JSON data isn’t part of a REST API, such as documents in MongoDB, files on a disk, or messages, JSON Schema provides a way to specify what the JSON will look like in a way that covers all the possibilities better than making example documents.

JSON Schema is itself a JSON document, with pre-defined properties and types. For example, a simple JSON schema might look like this:

{
    "description": "Mailing address schema",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": ["name", "zip"],
    "properties": {
        "name": {
            "description": "Full name of the recipient",
            "type": "string"
        },
        "addressType": {
            "description": "Whether the address is a residence, a business, or a post office box",
            "enum": ["residence", "business", "pobox"]
        },
        "street": {
            "description": "Street address and number",
            "type": "string"
        },
        "city": {
            "description": "City",
            "type": "string"
        },
        "state": {
            "description": "State code",
            "type": "string",
            "minLength": "2",
            "maxLength": "2"
        },
        "zip": {
            "description": "Zip code",
            "type": "string",
            "pattern": "^[0-9]{5}(?:-[0-9]{4})?$"
        }
    }
}

If well indented, the above is moderately readable, if verbose. However, it is preferable to have tools to make it easier to navigate, and also to perform validation. Validation at runtime of course depends on the language, but the Python jsonschema library provides an easy means of testing JSON content to make sure the schema does what it’s supposed to.

For navigation, there is a great tool called Docson that generates an interactive web page from a JSON schema. Docson is a JavaScript library, so it can dynamically generate the documentation from any JSON schema file it can fetch. Of course, because of cross-origin scripting concerns, there are some limits as to what it can fetch, so the best approach is to grab a local copy rather than serving from a Content Delivery Network (CDN).

With a copy downloaded and running, an example JSON file in the same directory as Docson’s index.html, and using Python’s SimpleHTTPServer:

$ python -m SimpleHTTPServer 9999

Docson produces a nice looking documentation page: