This package wraps is-my-json-valid using V8 to do JSON schema validation in R.

You need a JSON schema file; see json-schema.org for details on writing these. Often someone else has done the hard work of writing one for you, and you can just check that the JSON you are producing or consuming conforms to the schema.

The examples below come from the JSON schema website

They describe a JSON based product catalog, where each product has an id, a name, a price, and an optional set of tags. A JSON representation of a product is:

The schema that they derive looks like this:

This ensures the types of all fields, enforces presence of id, name and price, checks that the price is not negative and checks that if present tags is a unique list of strings.

There are two ways of passing the schema in to R; as a string or as a filename. If you have a large schema loading as a file will generally be easiest! Here’s a string representing the schema (watch out for escaping quotes):

Create a validator:

If we’d saved the json to a file, this would work too:

The returned object is a function that takes as its first argument a json string, or a filename of a json file. The empty list will fail validation because it does not contain any of the required fields:

## [1] FALSE

To get more information on why the validation fails, add verbose = TRUE:

## [1] FALSE
## attr(,"errors")
##        field     message
## 1    data.id is required
## 2  data.name is required
## 3 data.price is required

The attribute “errors” is a data.frame and is present only when the json fails validation. The error messages come straight from is-my-json-valid and they may not always be that informative.

Alternatively, to throw an error if the json does not validate, add error = TRUE to the call:

## Error: 3 errors validating json:
##  - data.id: is required
##  - data.name: is required
##  - data.price: is required

And to continue validating after the first error, pass greedy = TRUE:

## [1] FALSE
## attr(,"errors")
##        field     message
## 1    data.id is required
## 2  data.name is required
## 3 data.price is required

which will sometimes show more errors.

The JSON from the opening example works:

## [1] TRUE

But if we tried to enter a negative price it would fail:

## [1] FALSE
## attr(,"errors")
##        field              message
## 1 data.price is less than minimum

…or duplicate tags:

## [1] FALSE
## attr(,"errors")
##       field        message
## 1 data.tags must be unique

or just basically everything wrong:

## [1] FALSE
## attr(,"errors")
##         field              message
## 1     data.id    is the wrong type
## 2   data.name    is the wrong type
## 3  data.price is less than minimum
## 4   data.tags       must be unique
## 5 data.tags.2    is the wrong type

The data.tags.2 name comes from within the is-my-json-valid source, and may be annoying to work with programmatically.

There is also a simple interface where you take the schema and the json at the same time:

## [1] TRUE