JSON Schema Basics

Beginner
6 min read
Updated: Nov 2026
Get started

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.

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

Properties and Required

Each key under properties is itself a schema for that field. Put names in required when the instance must include them.

product.schema.json
{
  "$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"]
}

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.

JSON
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);
Try It Yourself

How It Works

  1. $schema points at the draft dialect.
  2. type: "object" requires an object instance.
  3. title is documentation only.

Example 2 — Check required keys

A tiny check that required names exist on the instance.

JSON
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));
Try It Yourself

How It Works

  1. required lists keys that must be present.
  2. Optional keys stay out of that array.
  3. Full validators also check each property's type.

Example 3 — exclusiveMinimum on price

Prices must be greater than zero.

JSON
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));
Try It Yourself

How It Works

  1. exclusiveMinimum: 0 rejects 0 and negatives.
  2. Use minimum if zero should be allowed.
  3. 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.

Next: Schema Types

Learn how the type keyword maps to JSON values.

Schema Types tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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