How I validate JSON with a schema before blaming the API
· 4 min read
When an API receives JSON, it usually expects a certain shape. If one field is missing or one value has the wrong type, the request may fail.
A JSON Schema describes that expected shape.
The JSON Schema Validator lets you paste a JSON document and a schema, then see exactly which fields do not match.
The simple idea
JSON is the data:
{
"name": "Ana",
"age": 30
}
JSON Schema is the rulebook:
{
"type": "object",
"required": ["name", "age"],
"properties": {
"name": { "type": "string" },
"age": { "type": "number" }
}
}
The validator checks whether the data follows the rulebook.
Step 1: start with the top-level type
Most API payloads are objects:
{
"type": "object"
}
This means the JSON should look like:
{
"key": "value"
}
not like:
[1, 2, 3]
or:
"hello"
Start with the top-level type before adding details.
Step 2: define properties
Properties describe fields inside an object.
{
"type": "object",
"properties": {
"email": { "type": "string" },
"age": { "type": "number" }
}
}
This says:
emailshould be a string;ageshould be a number.
It does not yet say those fields are required.
Step 3: mark required fields
Use required to say which fields must exist:
{
"type": "object",
"required": ["email"],
"properties": {
"email": { "type": "string" },
"age": { "type": "number" }
}
}
Now this is valid:
{
"email": "ana@example.com"
}
But this is not:
{
"age": 30
}
because email is missing.
Step 4: check strings, numbers, booleans, and null
Common schema types:
string
number
integer
boolean
object
array
null
Be careful with numbers stored as strings.
This JSON value is a number:
10000
This one is a string:
"10000"
Some APIs require amounts as strings to preserve formatting. Others require numbers. The schema should match the API contract.
Step 5: validate nested objects
Nested objects need nested rules.
Payload:
{
"user": {
"name": "Ana",
"email": "ana@example.com"
}
}
Schema:
{
"type": "object",
"required": ["user"],
"properties": {
"user": {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string" },
"email": { "type": "string" }
}
}
}
}
The structure of the schema follows the structure of the JSON.
Step 6: validate arrays
For arrays, define the item shape.
Payload:
{
"items": [
{ "sku": "A1", "qty": 2 },
{ "sku": "B2", "qty": 1 }
]
}
Schema:
{
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"required": ["sku", "qty"],
"properties": {
"sku": { "type": "string" },
"qty": { "type": "integer" }
}
}
}
}
}
This checks every item in the array.
Step 7: read the error path
A good validator tells you where the problem is.
An error path may look like:
/items/0/qty
Plain meaning:
- go to
items; - take item
0, the first item; - check
qty.
If the error says that value should be an integer, you know exactly which field to fix.
The JSON Schema Validator shows these paths so you do not have to search the whole document manually.
Step 8: use schema for API debugging
When an API rejects a payload, I check:
- Is the JSON valid?
- Does it match the schema?
- Are required fields present?
- Are values the right types?
- Are nested objects in the expected place?
- Are arrays using the expected item shape?
This separates two problems:
- invalid JSON syntax;
- valid JSON with the wrong shape.
Those are different bugs.
Step 9: keep the schema close to examples
A schema is easier to trust when it sits next to example payloads.
For documentation, I like having:
- one valid example;
- one invalid example;
- the schema;
- notes for required and optional fields.
This helps frontend, backend, and QA people talk about the same contract.
My JSON Schema checklist
When writing or testing a schema, I check:
- What is the top-level type?
- Which fields are required?
- What type should each field be?
- Are nested objects described?
- Are arrays and their items described?
- Are numbers and strings used intentionally?
- Does the validator show clear error paths?
- Does the schema match the API documentation?
JSON Schema is not only for strict validation. It is also a way to make an API contract visible and testable.
Comments
Comments are welcome — please read the comment policy first. Powered by giscus and GitHub Discussions.