Syntax validation tells you whether JSON is structurally correct. Schema validation tells you whether the JSON contains the right data — the right fields, the right types, and the right constraints. Both are necessary for robust APIs.
What is JSON Schema?
JSON Schema is a vocabulary for describing the shape of JSON data. A schema is itself a JSON document that specifies what properties an object must have, what types they must be, which fields are required, and what additional constraints apply (minimum, maximum, pattern matching, etc.).
The JSON Schema specification is maintained at json-schema.org. Validators are available for every major programming language.
Basic schema structure
Here's a simple schema for a user object:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": ["id", "name", "email"],
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"email": {
"type": "string",
"format": "email"
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 150
},
"roles": {
"type": "array",
"items": { "type": "string" },
"minItems": 1
}
},
"additionalProperties": false
}
Key elements:
"$schema"— declares which JSON Schema draft this schema uses"type": "object"— the top-level value must be a JSON object"required"— array of keys that must be present"properties"— defines the allowed keys and their types"additionalProperties": false— rejects keys not listed in properties (strict mode)
Validating with Python (jsonschema library)
# pip install jsonschema
import json
import jsonschema
schema = {
"type": "object",
"required": ["id", "name", "email"],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"email": { "type": "string", "format": "email" }
}
}
# Valid data
user = {"id": 1, "name": "Alice", "email": "alice@example.com"}
jsonschema.validate(user, schema) # No error = valid
# Invalid data — id is a string, not integer
bad_user = {"id": "one", "name": "Alice", "email": "alice@example.com"}
try:
jsonschema.validate(bad_user, schema)
except jsonschema.ValidationError as e:
print(e.message)
# 'one' is not of type 'integer'
Validating with Node.js (ajv library)
// npm install ajv
const Ajv = require('ajv');
const ajv = new Ajv();
const schema = {
type: 'object',
required: ['id', 'name', 'email'],
properties: {
id: { type: 'integer' },
name: { type: 'string' },
email: { type: 'string', format: 'email' }
},
additionalProperties: false
};
const validate = ajv.compile(schema);
const user = { id: 1, name: 'Alice', email: 'alice@example.com' };
const valid = validate(user);
if (!valid) {
console.log(validate.errors);
} else {
console.log('Valid!');
}
Ajv is the most widely used JSON Schema validator in the JavaScript ecosystem. It compiles schemas for performance, which matters when validating thousands of requests per second in a production API.
When schema validation matters most
Schema validation is most valuable at API boundaries: incoming HTTP request bodies, webhook payloads from third-party services, and data imported from external files. Validating at the boundary catches bad data before it reaches your database or business logic, which is far easier to handle than discovering corrupted data after the fact.
It's generally not worth the performance overhead to schema-validate data that your own code has produced internally — trust your serialization layer instead.
Quick syntax validation first
Before schema validation can work, the JSON must be syntactically valid. Use the JSON Validator to confirm the JSON parses correctly, then apply schema validation to check the data shape. The validator reports the exact line and character position of any syntax error, which is faster than debugging from a stack trace.