validatejsonr

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)

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

print(result$message)

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)

print(result$message)

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:")
print(result$schema)

cat("JSON File that the function was called with:")
print(result$jsonfile)

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)

print(result$message)

Using validate_* on missing files throws and error.

result = tryCatch({
    validate_jsonfile_with_schemafile("missing", schemafile)
}, error = function(e) {
    print(e)
})

result = tryCatch({
validate_jsonfile_with_schemafile(validjson, "missing")
}, error = function(e) {
    print(e)
})

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)
print(result$message)

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)
result <- validate_result <- validate_json_with_schema(json_code, schema_code)
print(result$value)
print(result$message)
print(result$schema)


Try the validatejsonr package in your browser

Any scripts or data that you put into this service are public.

validatejsonr documentation built on May 1, 2019, 8:02 p.m.