How to Validate JSON Against a Schema (With AJV, In Your Browser)
A JSON document can be well-formed and still be wrong. This guide explains JSON Schema validation, how AJV catches contract violations, and how to check your data with precise, path-level errors.
Valid JSON is not the same thing as correct JSON. A payload can parse perfectly well and still be missing a required field, have a string where a number belongs, or pass an age of -5. Catching that class of bug requires more than a parser - it requires a schema, and something that enforces it.
That is what the JSON Schema Validator does: it checks a JSON document against a JSON Schema using AJV, the same battle-tested validation engine used across the JavaScript ecosystem, and reports exactly which property broke which rule. This guide walks through how schema validation works, how to write a schema that actually catches bugs, and how to fix the errors AJV reports.
What is JSON Schema validation?
JSON Schema is a vocabulary for describing the shape of JSON data: which properties exist, what type each one must be, which ones are required, and what range or pattern their values must fall within. Validation is the act of checking a real document against that description and reporting every place it deviates.
This is different from JSON syntax checking. A JSON Validator confirms a document is well-formed JSON at all - correct brackets, quotes, and commas. A schema validator goes a level deeper and confirms the *content* is correct: the right types, the right required fields, values inside acceptable bounds. You typically want both, in that order.
Why schema validation matters
Most real-world bugs in JSON-driven systems are not syntax errors - they are contract violations. An API returns a string where the frontend expects a number. A config file omits a field the deploy script assumes exists. A form submits an age of 15 to an endpoint that requires 18 or older. None of these break JSON parsing, but all of them break the application.
- Catch bad data before it spreads - reject a malformed request at the boundary instead of debugging a crash three services downstream.
- Document your data contract - a schema is executable documentation that never goes stale the way a comment can.
- Get exact, actionable errors - instead of a generic failure, you learn the precise property and rule that failed.
- Test generated or third-party data - confirm a payload from another team, an LLM, or a public API actually matches what you were promised.
How AJV validates a document
AJV (Another JSON Validator) compiles a JSON Schema into a fast validation function and then runs your data through it, keyword by keyword. For every property, it checks the applicable rules - type, required, enum, pattern, minimum/maximum, minLength/maxLength, and more - and records a violation whenever one fails.
Crucially, AJV does not stop at the first problem. It walks the entire document and collects every violation it finds in one pass, each tagged with an instance path (where in your data the problem is) and the keyword that failed (which rule was broken). That is what makes the JSON Schema Validator useful for real debugging rather than a slow one-error-at-a-time loop.
Your schema and data never leave the browser
Both the schema you write and the document you are checking can contain sensitive structure - internal field names, customer records, API contracts you have not shipped yet. The JSON Schema Validator runs AJV entirely client-side, so neither the schema nor the data is ever uploaded, logged, or stored on a server.
Step-by-step: validate a document against a schema
- Open the JSON Schema Validator and paste your JSON Schema into the schema panel - or generate a starting draft with the JSON Schema Generator from a sample document.
- Paste the JSON document you want to check into the data panel.
- Run the validation. AJV checks every applicable keyword and lists each violation with its instance path.
- Fix each reported issue in your data (or loosen the rule in the schema if the rule itself was too strict).
- Re-run validation until the document passes with no errors.
Once a schema is passing reliably, keep it alongside your fixtures or config so you can re-check future data the same way, without rewriting the checks by hand each time.
A worked example
Here is a minimal schema that requires an age field to be an integer of at least 18, checked against a document that violates that rule:
A document that fails validation
The schema requires age >= 18; the data supplies 15.
Input
// schema
{
"type": "object",
"properties": {
"age": { "type": "integer", "minimum": 18 }
},
"required": ["age"]
}
// data
{ "age": 15 }Output
Invalid: /age must be >= 18 (minimum).Notice the error names the exact instance path (/age) and the exact keyword that failed (minimum). In a document with dozens of properties, that precision is the difference between a two-second fix and a long manual comparison.
Common use cases
- Enforcing a contract on incoming API request bodies before they reach business logic.
- Verifying config files conform to an agreed schema before a deploy runs.
- Checking that data generated by a script, pipeline, or LLM actually matches the structure you specified.
- Catching missing required fields or wrong types during code review of a fixture change.
- Validating user-submitted JSON - webhooks, form payloads, uploaded settings - against documented rules.
Common mistakes when writing and using schemas
Forgetting the required array
Declaring a property under properties does not make it mandatory. If a field must always be present, it also needs to be listed in the schema's required array - otherwise AJV will silently accept documents that omit it entirely.
Types that are too loose
A type: "object" schema with no properties or required will pass almost anything. Schemas are only as useful as the constraints you actually write into them - be specific about types, formats, and bounds rather than leaving fields open-ended.
Confusing syntax errors with schema violations
If your input is not valid JSON at all - a trailing comma, an unquoted key - that is a parsing problem, not a schema violation, and no validator can check content until the syntax is fixed first. Run a JSON Formatter or validator pass first if you are unsure whether the problem is structural or a rule violation.
Ignoring additionalProperties
By default, JSON Schema allows extra properties beyond the ones you declared. If your contract needs to reject unexpected fields - a common requirement for strict API validation - set additionalProperties: false explicitly; it is not the default behavior.
Best practices for schema validation
- Start from real data: generate a draft schema with the JSON Schema Generator and then tighten the constraints that matter, rather than writing a schema from scratch.
- Keep schemas in version control next to the code or config they validate, so contract changes are reviewed like any other change.
- Prefer specific constraints (
enum,pattern,minimum,maxLength) over baretypechecks whenever the domain has real limits. - Fix every reported violation in one pass - since AJV returns all errors at once, there is no need to re-run after each single fix.
- Validate untrusted or sensitive input locally rather than sending it to a third-party service, especially for payloads containing tokens or personal data.
Related tools
Infer a JSON Schema from a sample JSON document.
Validate JSON syntax and pinpoint the exact line and column of any error.
Query JSON with JSONPath expressions and see live results.
Pretty-print JSON with configurable indentation and instant validation.
Related guides
Writing a JSON Schema by hand is slow and error-prone. This guide shows how inferring one from a real example gets you a working draft in seconds, plus how to refine it correctly.
A single misplaced comma can break an entire API request. This guide explains how JSON validation works, the mistakes it catches, and how to find the exact error location in seconds.
JSONPath lets you extract exactly the values you need from a JSON document without writing a parser. This guide covers the syntax and shows how to test expressions instantly.
Minified JSON is unreadable and hard to debug. This guide covers how JSON formatting works, when to use it, common mistakes, and how to pretty-print JSON without uploading it anywhere.