JSONHint

Preparing your workspace…

JSON Schema: test an API contract

A worked example with practical checks and limitations.

By JSONHint · Updated

JSON syntax checks whether a document can be parsed. A schema expresses additional expectations about its shape. A valid JSON string can still be the wrong type for an order id, and an empty object can still be missing required data.

A small order contract

{"type":"object","required":["id","paid"],"properties":{"id":{"type":"integer","minimum":1},"paid":{"type":"boolean"}},"additionalProperties":false}

Use this schema in the validator with the following inputs. Change one thing at a time so that a failure tells you which rule was tested.

DataExpected result
{"id":42,"paid":false}Passes the supported checks
{"id":"42","paid":false}id has the wrong type
{"id":42}Missing required paid field
{"id":0,"paid":false}id is below the minimum
{"id":42,"paid":false,"debug":true}Unexpected property debug

Missing, null and false are different

A required field must be present. That does not mean any value is accepted: null is not a Boolean, and false is a valid Boolean rather than a missing value. Keep presence checks separate from type checks when designing the contract. Optional properties need not appear unless they are listed in required.

Know this tool's limits

JSONHint implements a lightweight subset: type (including integer), enum, required, properties, additionalProperties, string length and pattern, numeric minimum and maximum, and array items. It does not implement full draft conformance, remote references, composition such as allOf, or format assertions. Unsupported keywords may be ignored, so a green result is not a certificate that an arbitrary schema is satisfied.

For production validation, select a complete implementation that supports your schema dialect and test it with the official test suite. In this tool, string lengths use JavaScript string length and patterns use JavaScript regular expressions. Those details matter for non-BMP Unicode and dialect-specific expressions. Do not use untrusted complex patterns as a security boundary.

Keep the contract with its tests

Store valid and invalid fixtures alongside the schema in your project. Test missing fields, unexpected keys, boundary values and null separately. A structural schema cannot decide whether an account exists or a payment is authorized; those checks belong to application logic.

Read the JSON Schema object reference for the specification-level behavior and compare it with the supported subset before relying on a result.

Found an error? Send a reproducible example or a source correction.