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).
validatejsonr support four main usage scenarios.
The result of a validation contains four fields
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) })
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)
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)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.