JSON Schema Basics

Definition
Definition and Usage
Start with $schema, optional $id, annotations like title, and a root type. Then describe fields under properties and list required keys.
Quick path.Declare the draft → set type: "object" → add properties → mark required fields.
Keywords
Core Definition Keywords
- $schema Draft URI, e.g.
https://json-schema.org/draft/2020-12/schema - $id Stable URI for this schema document
- title / description Human notes — not validation constraints
- type Root constraint: object, array, string, number, boolean, null, integer
Shape
Properties and Required
Each key under properties is itself a schema for that field. Put names in required when the instance must include them.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"title": "Product",
"type": "object",
"properties": {
"productId": { "type": "integer" },
"productName": { "type": "string" },
"price": { "type": "number", "exclusiveMinimum": 0 }
},
"required": ["productId", "productName", "price"]
}
Code
Examples
Use View Output for a quick result, or Try It Yourself to edit and run in the browser.
Example 1 — Root object schema
Build the skeleton with $schema and type.
const schema = {
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: "https://example.com/product.schema.json",
title: "Product",
type: "object"
};
console.log(schema.$schema.includes("2020-12"));
console.log(schema.type);
console.log(schema.title);
How It Works
$schema points at the draft dialect.type: "object" requires an object instance.title is documentation only.
Example 2 — Check required keys
A tiny check that required names exist on the instance.
const schema = {
type: "object",
required: ["productId", "productName", "price"],
properties: {
productId: { type: "integer" },
productName: { type: "string" },
price: { type: "number" }
}
};
function missingRequired(instance, required) {
return required.filter(function (key) {
return !Object.prototype.hasOwnProperty.call(instance, key);
});
}
const good = { productId: 1, productName: "Door", price: 12.5 };
const bad = { productId: 1, productName: "Door" };
console.log(missingRequired(good, schema.required));
console.log(missingRequired(bad, schema.required));
How It Works
required lists keys that must be present.- Optional keys stay out of that array.
- Full validators also check each property's type.
Example 3 — exclusiveMinimum on price
Prices must be greater than zero.
const priceSchema = { type: "number", exclusiveMinimum: 0 };
function priceOk(value, schema) {
if (typeof value !== "number" || Number.isNaN(value)) return false;
return value > schema.exclusiveMinimum;
}
console.log(priceOk(12.5, priceSchema));
console.log(priceOk(0, priceSchema));
console.log(priceOk(-1, priceSchema));
How It Works
exclusiveMinimum: 0 rejects 0 and negatives.- Use
minimum if zero should be allowed. - This mirrors the official product catalog example.
Frequently Asked Questions
$schema declares which JSON Schema draft the document follows so validators apply the correct rules.
$id sets a URI for the schema so other schemas can $ref it and tools can identify it uniquely.
No. They are annotations for humans and tooling. They do not constrain the instance.
List its name in the required array at the same object level as properties.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful