JSON Schema validation turns an API payload from an informal blob of data into something you can check consistently. This guide explains how to validate JSON against a schema, interpret missing-field and type errors, detect unexpected properties, compare validator capabilities, and use a repeatable review checklist before changes reach production.
Overview
Valid JSON is not necessarily valid API data. A payload can use correct JSON syntax while still omitting a required field, sending a number as a string, or including a property that the receiving service does not understand. A JSON Schema describes the structure and constraints that a payload is expected to follow; a JSON Schema validator compares the instance document with those rules and reports failures.
A practical validation workflow has four layers:
- Syntax: Confirm that the document can be parsed as JSON. A trailing comma, unclosed string, or malformed escape sequence must be fixed first.
- Shape: Confirm that objects, arrays, strings, numbers, booleans, and null values appear in the expected places.
- Constraints: Check required properties, allowed values, string formats, numeric limits, array lengths, and nested objects.
- Compatibility: Check whether a schema change could break existing clients, stored data, or downstream integrations.
For a quick investigation, a browser-based JSON validation tool can be useful, especially when you need to test a sample payload against a schema. Do not paste credentials, access tokens, personal data, or confidential production payloads into an online validator unless its data-handling terms and deployment model are appropriate. For repeatable API schema testing, keep validation in your local development workflow and automated tests.
A small schema example
The following schema requires an object with a string id, a valid-looking email value in email, and an array of string roles. The additionalProperties setting rejects properties that are not declared.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"required": ["id", "email", "roles"],
"properties": {
"id": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" },
"roles": {
"type": "array",
"items": { "type": "string" },
"minItems": 1
}
}
}
A payload such as {"id":42,"email":"not-an-email","roles":[],"debug":true} can fail for several independent reasons: id has the wrong type, email does not satisfy the declared format, roles violates minItems, and debug is unexpected.
Checklist by scenario
When adding a new endpoint
- Write down the request and response examples before writing the schema.
- Declare the root type explicitly, usually
objectorarray. - Mark only genuinely mandatory fields as
required. An optional field should not be made mandatory simply because it appears in one example. - Define nested objects and array item types instead of validating only the top-level shape.
- Decide how unknown properties should be handled. Rejecting them can expose client mistakes; allowing them can make additive changes easier.
- Add valid, invalid, boundary, and minimal payloads to the test set.
When troubleshooting a failing request
- Capture the exact request body, not a manually retyped approximation.
- Validate JSON syntax before investigating schema errors.
- Read the error path, such as
customer.address.postcodeoritems[2].sku, to locate the failing value. - Compare the received type with the schema type. Common mismatches include
"12"versus12,"false"versusfalse, and a single object versus an array containing that object. - Check whether the validator treats formats such as email or date as assertions. Format behavior can vary by validator configuration.
- Inspect headers, authentication, and the actual request body separately. A valid payload can still fail because of transport or authorization problems. The cURL API debugging guide provides a complementary request-inspection workflow.
When changing an existing schema
- Run old valid payloads against the proposed schema.
- Test payloads produced by every supported client or service version.
- Be cautious when adding a required property, narrowing an enum, changing a type, or rejecting previously accepted properties.
- Consider whether consumers ignore unknown response fields or fail when they encounter them.
- Record the schema version and decide whether the endpoint needs an explicit compatibility strategy.
Draft and validator comparison
Draft support is not interchangeable. The schema’s $schema declaration identifies the intended vocabulary, but the validator must also support that draft and the keywords you use. Confirm compatibility in the tool’s documentation or test suite before relying on a feature.
| Schema draft | What to verify | Typical review concern |
|---|---|---|
| Draft 2020-12 | Support for its vocabulary, dynamic reference behavior, and newer composition features | Older validators may require configuration or may not support every keyword |
| Draft 2019-09 | Support for newer validation and annotation keywords | Tooling may support the draft partially rather than uniformly |
| Draft-07 | Compatibility with established libraries and CI integrations | A team may select it for broad legacy compatibility, but should document that choice |
| Draft-06 and earlier | Legacy keyword behavior and reference handling | Use only when an existing integration requires it, and test migration separately |
When comparing validators, check five capabilities: supported drafts, format checking, custom keywords, reference resolution, and error reporting. Also check whether the tool validates one payload interactively or can run in a build, test, or deployment pipeline. A convenient JSON validation online is useful for exploration; a command-line or library-based validator is usually better for repeatability.
What to double-check
- Required is not the same as non-empty. A required string may still be
""unless you addminLength. - Numbers and numeric strings differ. Decide whether conversion belongs in the client, server, or validation layer. Do not assume a validator will coerce values.
- Null is a separate type. If a field may be null, declare that possibility explicitly using the syntax supported by the selected draft and validator.
- Formats may need explicit enforcement. Treat dates, email addresses, URIs, and similar formats as a documented validation decision rather than an assumption.
- Arrays need item rules. Validate the array itself and each item. Add uniqueness or length constraints where the API requires them.
- References must resolve. Schemas split across files or components can appear correct while failing in a validator that cannot locate a referenced definition.
- Compositions need negative tests. With
oneOf,anyOf, andallOf, test values that match none, one, and more than one branch where relevant.
Keep a small payload review set beside the schema: one minimal valid example, one complete valid example, one example for each conditional branch, and one failure example for each important rule. This makes contract changes easier to review than relying on a single large fixture.
Common mistakes
Using examples as validation rules
An example illustrates a possible payload; it does not automatically define required fields, types, or allowed values. Express those rules with schema keywords and test them directly.
Making every property required
Overly strict schemas can reject legitimate partial updates, phased rollouts, or responses where a value is conditionally available. Model the API contract rather than the most complete object your application can produce.
Ignoring unknown properties without a policy
Silently accepting extra properties can hide misspellings. Rejecting every extra property can make additive evolution difficult. Choose a policy per boundary and document whether it applies to requests, responses, or both.
Validating only in production
Runtime validation is useful at an API boundary, but it should not be the only check. Add schema validation to unit tests, integration tests, contract tests, or CI so failures are found before deployment.
Confusing schema failures with transport failures
Inspect HTTP status codes, headers, authentication, content type, redirects, and the raw body separately. If the problem involves query construction rather than JSON, a formatting and review workflow such as the SQL formatter guide may be the more relevant tool.
When to revisit
Revisit this checklist whenever you add an endpoint, change a request or response contract, upgrade a validator, change the declared schema draft, or investigate an integration failure. It is also worth reviewing before a planned release or seasonal planning cycle, when multiple services may change on different schedules.
At each review, confirm the selected draft, validator configuration, format behavior, reference resolution, unknown-property policy, and test fixtures. Run the previous payload set against the new schema, then add a regression test for every defect you discover. If the API has several consumers, publish the compatibility decision with the schema version so clients know whether they can upgrade independently.
Final API payload review checklist:
- Parse the payload as JSON.
- Validate the root and nested types.
- Check required, nullable, and conditionally required fields.
- Check formats, enums, lengths, ranges, and array items.
- Review unexpected properties and schema references.
- Test valid, invalid, boundary, and backward-compatibility cases.
- Confirm the validator’s draft and configuration match the schema.
- Run the checks automatically before merging or deploying.
Used this way, JSON Schema is more than a JSON validator: it becomes a shared review point between API designers, service owners, and client developers. Keep the schema, examples, and automated tests together, and update all three when the contract changes.