validatejsonr

Leif Jonsson

2016-10-19

validatejsonr is an R wrapper around an efficient C++ JSON Schema validator called ‘rapidjson’. You can read more about ‘rapidjson’ here: https://github.com/miloyip/rapidjson. JSON files can be well-formed and/or valid. A well-formed JSON file conforms to the syntactic requirements of JSON. Additionaly a valid JSON file conforms to a specified JSON Schema.

validatejsonr via ‘rapidjson’ supports JSON Schema Draft v4. 2016-09-09: ‘RapidJSON’ passed 262 out of 263 tests in JSON Schema Test Suite (JSON Schema draft 4).

Usage Scenarios

validatejsonr support four main usage scenarios.

Validation Results

The result of a validation contains four fields

Examples

File - File Example

Example using a schema file on a well-formed and valid JSON file returns a 0 value.

library(validatejsonr)
validjson     <- system.file("extdata", "item-3.json", package = "validatejsonr")
schemafile    <- system.file("extdata", "schema.json", package = "validatejsonr")

result <- validate_jsonfile_with_schemafile(validjson, schemafile)
print(result$value)
## [1] 0
malformedjson <- system.file("extdata", "malformed-json.json", package = "validatejsonr")

print(result$message)
## [1] "JSON file is valid with respect to Schema"

Using validate_* on a syntactically invalid JSON file returns a 1XX error.

malformedjson <- system.file("extdata", "malformed-json.json", package = "validatejsonr")

result <- validate_jsonfile_with_schemafile(malformedjson, schemafile)
print(result$value)
## [1] 100
print(result$message)
## [1] "JSON Input is not well-formed JSON. Error(offset 12): Missing a colon after a name of object member."

In addition to containing the resulting value the result also contains the input Schema and JSON file.

cat("Schema that the function was called with:")
## Schema that the function was called with:
print(result$schema)
## [1] "/private/var/folders/9x/c5z4mr412nv22vrg774_k44s7nhqwk/T/RtmpqkcQTT/Rinst4f2e6efc7e3d/validatejsonr/extdata/schema.json"
cat("JSON File that the function was called with:")
## JSON File that the function was called with:
print(result$jsonfile)
## [1] "/private/var/folders/9x/c5z4mr412nv22vrg774_k44s7nhqwk/T/RtmpqkcQTT/Rinst4f2e6efc7e3d/validatejsonr/extdata/malformed-json.json"

Using validate_* on a syntactically correct JSON but invalid (file does not conform to schema) file returns a 2XX error.

invalidjson  <- system.file("extdata", "item-2.json", package = "validatejsonr")

result <- validate_jsonfile_with_schemafile(invalidjson, schemafile)

print(result$value)
## [1] 200
print(result$message)
## [1] "Invalid schema point: #Invalid keyword: required Invalid document point: #"

Using validate_* on missing files throws and error.

result = tryCatch({
    validate_jsonfile_with_schemafile("missing", schemafile)
}, error = function(e) {
    print(e)
})
## <Rcpp::exception in eval(substitute(expr), envir, enclos): Cannot access JSON file:missing>
result = tryCatch({
validate_jsonfile_with_schemafile(validjson, "missing")
}, error = function(e) {
    print(e)
})
## <Rcpp::exception in eval(substitute(expr), envir, enclos): Cannot access JSON Schema file:missing>

More Examples

JSON String - Schema File Example

Using the string API, string JSON, schema file.

json_code <- "{\"category\": \"book\", \"price\": 25,  \"title\": \"abrakadabra\"}"
result <- validate_result <- validate_json_with_schemafile(json_code, schemafile)
print(result$value)
## [1] 0
print(result$message)
## [1] "JSON string is valid with respect to Schema"

JSON string - Schema string Example

Using the string API, string JSON, string schema.

json_code <- "{\"category\": \"book\", \"price\": 25,  \"title\": \"abrakadabra\"}"
schema_code     <- readChar(schemafile, file.info(schemafile)$size)
print(schema_code)
## [1] "{\n    \"properties\": {\n      \"category\": {\n        \"enum\": [\n          \"album\",\n          \"book\",\n          \"other\",\n          \"video\"\n        ]\n      },\n      \"description\": {\n        \"type\": \"string\"\n      },\n      \"price\": {\n        \"exclusiveMinimum\": true,\n        \"minimum\": 0.0,\n        \"type\": \"number\"\n      },\n      \"title\": {\n        \"maxLength\": 200,\n        \"minLength\": 1,\n        \"type\": \"string\"\n      }\n    },\n    \"required\": [\n      \"category\",\n      \"price\",\n      \"title\"\n    ],\n    \"type\": \"object\"\n}\n\n"
result <- validate_result <- validate_json_with_schema(json_code, schema_code)
print(result$value)
## [1] 0
print(result$message)
## [1] "JSON string is valid with respect to Schema"
print(result$schema)
## [1] "{\n    \"properties\": {\n      \"category\": {\n        \"enum\": [\n          \"album\",\n          \"book\",\n          \"other\",\n          \"video\"\n        ]\n      },\n      \"description\": {\n        \"type\": \"string\"\n      },\n      \"price\": {\n        \"exclusiveMinimum\": true,\n        \"minimum\": 0.0,\n        \"type\": \"number\"\n      },\n      \"title\": {\n        \"maxLength\": 200,\n        \"minLength\": 1,\n        \"type\": \"string\"\n      }\n    },\n    \"required\": [\n      \"category\",\n      \"price\",\n      \"title\"\n    ],\n    \"type\": \"object\"\n}\n\n"