JSON Schema validation turns an API payload from “looks reasonable” into a testable contract. This guide provides a repeatable workflow for validating JSON, diagnosing failures, choosing online or local developer tools, and documenting request and response expectations.
Overview
JSON syntax validation and JSON Schema validation are different checks. A JSON formatter can tell you whether braces, commas, strings, and values form valid JSON. A JSON Schema validator goes further: it compares the parsed data with rules for types, required properties, nesting, allowed values, formats, and additional properties.
For example, this payload is valid JSON:
{
"id": 42,
"email": "dev@example.test"
}
It may still fail a schema that requires id to be a string, requires a name property, or disallows fields that are not explicitly defined. The reliable workflow is therefore:
- Confirm that the document is valid JSON.
- Confirm that the schema itself is valid and declares its intended draft.
- Validate the payload against the schema.
- Read the first useful error in context, then check related errors.
- Record the contract in API documentation and automated tests.
For a broader response-inspection process, see the Developer Debug Toolkit. If the contract is maintained as an OpenAPI document, the OpenAPI specification guide can help connect schemas with endpoints and tests.
Checklist by scenario
When validating a single payload online
- Remove credentials, tokens, personal data, and confidential business fields before pasting.
- Format the payload first so you can see its actual nesting and value types.
- Paste the complete schema, including its declared draft when required by the tool.
- Check whether the validator tests formats such as email, URI, or date strictly or only treats them as annotations.
- Copy the exact error path, such as
$.customer.address.postcode, into the issue or test report.
Online JSON validation is useful for a quick investigation, but it should not be the only protection for production data. Tool behavior, supported drafts, format handling, and privacy practices vary, so confirm those details before relying on a browser-based validator.
When adding validation to an application
- Select a library that supports the schema draft and language used by your application.
- Keep schemas in version control beside the API code or in a clearly owned contract repository.
- Validate both representative valid payloads and intentionally invalid payloads.
- Decide whether validation occurs at the HTTP boundary, inside a service, or at both layers.
- Return a stable error shape without exposing stack traces, secrets, or internal implementation details.
A practical tool-selection matrix looks like this:
| Option | Best fit | Confirm before adopting |
|---|---|---|
| Browser-based JSON Schema validator | One-off checks and learning | Data handling, draft support, format behavior, and error paths |
| Language library | Runtime request or response validation | Supported draft, strictness settings, performance, and error reporting |
| Command-line validator | Local scripts and CI checks | Exit codes, file-reference handling, and reproducible configuration |
| IDE extension | Authoring schemas and inspecting examples | Editor settings, schema association, and consistency with CI |
| OpenAPI tooling | Endpoint documentation and contract workflows | How the tool interprets reusable schemas and request versus response bodies |
When an API request fails validation
- Capture the request body sent over the wire rather than relying on the object before serialization.
- Compare the failing field with the schema’s
type,required,properties, and constraints. - Check whether a client converted a number to a string, omitted a null value, or serialized an array differently than expected.
- Test the smallest payload that still reproduces the failure.
- Confirm that the server and client are using the same schema version.
Pagination fields are a frequent source of contract drift. If an endpoint changes from page numbers to cursors, compare the payload rules with the patterns in the API pagination guide.
What to double-check
Types and required fields
JSON has a small set of value types: object, array, string, number, boolean, and null. A value that looks numeric may still be a string, such as "42" instead of 42. Also distinguish between a missing property and a property explicitly set to null. A schema must express the intended behavior rather than assuming these values are interchangeable.
The required keyword applies to property names in an object. It does not make every nested value non-null, and it does not automatically apply to objects inside an array. Define nested rules where the data actually appears.
Nested objects and arrays
Use properties to describe object members and items to describe array elements. If an array can contain more than one shape, document that deliberately with the schema’s composition features rather than leaving consumers to infer the format.
{
"type": "object",
"required": ["items"],
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"required": ["sku", "quantity"],
"properties": {
"sku": { "type": "string" },
"quantity": { "type": "integer", "minimum": 1 }
},
"additionalProperties": false
}
}
},
"additionalProperties": false
}
Formats and additional properties
Formats such as dates, email addresses, and URIs can improve documentation, but validators may offer different levels of enforcement. Test the exact validator configuration used in production. Likewise, decide whether unknown fields should be rejected, ignored, or preserved. Setting additionalProperties to false can catch typos, but it can also make additive API changes breaking if clients send newer fields to older services.
Common mistakes
- Validating only syntax: A formatted document is not necessarily contract-compliant.
- Using the wrong draft: Keywords and behavior can differ across JSON Schema drafts. Declare and test the intended draft.
- Confusing examples with rules: An example illustrates a payload; it does not constrain other values unless the schema does so.
- Forgetting response validation: Clients depend on response contracts too. Validate important responses in integration or contract tests.
- Returning vague errors: “Invalid request” is difficult to debug. Include a field path, a safe reason, and a machine-readable code.
- Changing schemas without compatibility review: Removing required fields, narrowing an enum, changing a type, or rejecting previously accepted properties may break consumers.
- Trusting one tool blindly: Compare local validation with the validator used by CI or the service boundary when behavior differs.
Schema errors often appear alongside authentication, serialization, or transport problems. If the body is missing or altered before validation, inspect the complete API response and request flow using the CORS troubleshooting guide where browser requests are involved. For token-related failures, use the JWT decoder and validator guide, while keeping real tokens out of public tools and logs.
When to revisit
Revisit your JSON Schema validation workflow before a new endpoint, a payload redesign, a client-library release, or a planned contract version change. It is also worth reviewing after changing validator libraries, upgrading language dependencies, modifying draft support, or moving checks from local development into CI.
Use this short maintenance checklist:
- Run valid and invalid fixtures against the current schema.
- Confirm that local, CI, and production validation use compatible settings.
- Review required fields, enum values, formats, nesting, and unknown-property behavior.
- Test the documented error response with malformed, incomplete, and incorrectly typed payloads.
- Update OpenAPI examples, generated client expectations, and changelog notes when the contract changes.
- Record the validator and schema version used by the test suite.
Keep this checklist with your API repository or team documentation. When an integration failure returns, start with the wire payload, validate it against the exact schema version, and work from the first field-specific error instead of editing the request by guesswork.