JSON Schema is a structural contract for JSON: it describes required fields, field types, arrays, nested objects, and optional format rules.
It is useful for API debugging, config validation, low-code forms, and data imports because it catches shape problems before data reaches business logic.
Start With a JSON Sample
{
"id": 1001,
"email": "user@example.com",
"roles": ["admin"],
"active": true
}This object contains a number, a string, an array, and a boolean. A Schema can describe each field and decide which fields are required.
{
"type": "object",
"required": ["id", "email", "roles", "active"],
"properties": {
"id": { "type": "number" },
"email": { "type": "string", "format": "email" },
"roles": {
"type": "array",
"items": { "type": "string" }
},
"active": { "type": "boolean" }
}
}Common JSON Schema Keywords
| Keyword | Purpose | Example |
|---|---|---|
| type | Restricts the base value type | object, array, string, number |
| properties | Describes fields in an object | email, roles, active |
| required | Lists fields that must exist | id, email |
| items | Describes array item types | each role is a string |
| format | Adds semantic hints for strings | email, uri, date-time |
How to Use It for API Validation
- Generate a first Schema from a realistic API sample.
- Refine required, format, enum, minLength, and other constraints from the API contract.
- Validate real request or response JSON against the Schema.
- Use the error path to locate the exact field that needs attention.
A generated Schema is only a starting point
A generator can observe only values present in the sample. It cannot know whether a field may be absent, whether a string is a closed enum, whether a number has limits, or what business type sits behind a lone null. Generated output is useful scaffolding, but the final constraints still need the API contract, database rules, and representative failure cases.
| Sample observation | What inference cannot know | Manual decision |
|---|---|---|
| A field appears every time | That does not prove it is required | Use the contract to decide required |
| The value is paid | Other states such as draft or failed are unseen | Add enum or oneOf |
| An array has one item | Other item shapes are unseen | Add more samples and review items |
| An object has no extra fields | Extensibility is unknown | Choose an additionalProperties policy |
Passing validation does not prove business correctness
Schema primarily verifies structure and the constraints you declared. A syntactically valid email is not proof that the mailbox exists, and a positive amount does not prove that an order may be refunded. Whether format is asserted can also depend on validator configuration. Use Schema at the input boundary to reject malformed data early, while cross-field rules, authorization, and business state remain application logic.
A team should also pin the JSON Schema draft and validator version. Keywords and reference behavior differ across drafts, so declare $schema where possible and include validation rules in API tests. That prevents production and documentation tooling from interpreting the same file differently.
Summary
JSON Schema turns a data shape into executable validation rules. That makes API and data pipeline errors easier to catch and explain.