I'm writing some acceptance tests for a java webapp that returns JSON objects. I would like to verify that the JSON returned validates against a schema. Can anyone suggest any tools for this?
The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.
JSON Schema Validation: The JSON Schema Validation specification is the document that defines the valid ways to define validation constraints. This document also defines a set of keywords that can be used to specify validations for a JSON API.
The JSON Tools project (Programmer's Guide) includes a tool to validate the contents of a JSON file using a JSON schema.
An alternative could be to validate running the (JavaScript) JSON Schema Validator using Rhino.
The json-schema-validator (currently in version 0.0.1, so it's in a pre-alpha state) worked pretty well for me. Be aware that it is not 100% feature complete but it could still correctly identify a lot of errors in my json content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With