jsonvalidate 1.1.0
Jun 25, 2019
Rich FitzJohn
2 minute read
Tags:

JSON is useful as a data-interchange format, due to the massive popularity of javascript. Basically every language supports reading and writing JSON now, so it makes a nice lowest common denominator.

When generating JSON from an application, it is useful to be able to describe how the data is structured - so if we return output like

{
    "id": 1,
    "name": "A green door",
    "price": 12.50,
    "tags": ["home", "green"]
}

We might describe it in words like:

The return value will be a dictonary with an element id that is a number, and an array tags, each element of which is a character, an element price whch is a number, and an element name which is a string

which is fine but impossible to write tools for. So JSON Schema was created for machine-readable descriptions of json objects. Naturally, JSON Schema is written in JSON (like XML Schema before it was written in XML).

A schema for the above structure might look like:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object",
    "properties": {
        "id": {
            "description": "The unique identifier for a product",
            "type": "integer"
        },
        "name": {
            "description": "Name of the product",
            "type": "string"
        },
        "price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        },
        "tags": {
            "type": "array",
            "items": {
                "type": "string"
            },
            "minItems": 1,
            "uniqueItems": true
        }
    },
    "required": ["id", "name", "price"]
}

With this, we can check that our return values have the expected type in automated testing, making our software more reliable when reused. We can also us it to validate incoming json and rely on a json schema validator to do the hard work of checking the data makes sense.

We have released to CRAN an update to the R package jsonvalidate, an R package for working with JSON schema. This update adds support for the ajv JSON Schema validator (in addition to the previously present is-my-json-valid).

We use jsonvalidate for validating responses returned by an HTTP API and for validating the intermediate representation used by odin

The ajv library includes support for draft 06 and 07 of JSON schema which includes lots of new features. Getting this into the package in a backward compatible way was possible with the help of Kara Woo and Ian Lyttle.




comments powered by Disqus