JSON Schema

Beginner
6 min read
Updated: Nov 2026
Overview

Definition and Usage

JSON Schema is a declarative language for annotating and validating JSON documents. A schema names expected types, required keys, nested structures, and constraints—so APIs and configs stay consistent.

Remember.

JSON is the data. JSON Schema is the contract that describes that data. Both are written as JSON text.

Why Use JSON Schema?

  • Validate Reject bad payloads before your app crashes
  • Document Describe fields for humans and code generators
  • Share Reuse schemas across services with $ref
  • Standardize One contract for OpenAPI, configs, and databases

Instance vs Schema

The instance is the JSON you store or send. The schema describes what that instance must look like.

Instance (data)
product.json
{
  "productId": 1,
  "productName": "A green door",
  "price": 12.5
}

Real payload from an API or file.

Schema (contract)
product.schema.json
{
  "type": "object",
  "required": ["productId", "productName", "price"],
  "properties": {
    "productId": { "type": "integer" },
    "productName": { "type": "string" },
    "price": { "type": "number", "exclusiveMinimum": 0 }
  }
}

Describes shape; does not store the product itself.

Examples

Use View Output for a quick result, or Try It Yourself to edit and run in the browser.

Example 1 — Blank schema allows anything

An empty object schema constrains nothing.

JSON
const schema = {};
const instance = { anything: true, nested: [1, 2] };

// A blank schema accepts every JSON value.
console.log("schema keys:", Object.keys(schema).length);
console.log("instance OK for blank schema:", true);
console.log(JSON.stringify(instance));
Try It Yourself

How It Works

  1. An empty schema {} adds no constraints.
  2. Any valid JSON instance is acceptable.
  3. Add keywords like type to start restricting values.

Example 2 — type constrains a value

The type keyword is the first common constraint.

JSON
const schema = { type: "string" };
const ok = "hello";
const bad = 42;

function matchesType(value, type) {
  if (type === "string") return typeof value === "string";
  if (type === "number") return typeof value === "number" && !Number.isNaN(value);
  return false;
}

console.log(matchesType(ok, schema.type));
console.log(matchesType(bad, schema.type));
Try It Yourself

How It Works

  1. type: "string" means the instance must be a JSON string.
  2. Numbers fail that check.
  3. Real validators implement the full draft rules; this is the idea.

Example 3 — Name the two documents

Keep instance and schema roles clear in code.

JSON
const instance = { name: "Ada", age: 36 };
const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "integer" }
  }
};

console.log("instance:", JSON.stringify(instance));
console.log("schema type:", schema.type);
console.log("property keys:", Object.keys(schema.properties).join(", "));
Try It Yourself

How It Works

  1. The instance holds the real values.
  2. The schema only describes allowed shapes.
  3. Validators take both documents as input.

Frequently Asked Questions

JSON Schema is a vocabulary written in JSON that describes the expected shape, types, and constraints of other JSON documents so you can validate and document them.
No. Data (the instance) is the payload. The schema is a separate JSON document that describes what valid instances look like.
New projects usually target draft 2020-12 (or 2019-09). Older APIs may still use draft-07. Set $schema so validators know which rules apply.
Yes for full validation. Browsers have JSON.parse, but JSON Schema needs a validator (Ajv, jsonschema, online tools, or your language’s library).

Next: Create a Schema

Write a product schema with $schema, type, and properties.

Create a Schema 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