JSON Schema Validate

Beginner
6 min read
Updated: Nov 2026
Guides

Definition and Usage

A validator takes a schema and an instance and reports whether the instance satisfies every applicable keyword. Production apps use a draft-compliant library; below you will try a minimal educational checker for type and required.

Order of checks.

Parse JSON first. Then validate against the schema. Invalid JSON never reaches schema rules.

Validation Flow

  • 1. Parse JSON.parse the instance (and schema) text
  • 2. Validate Run the draft rules for each keyword
  • 3. Result Pass, or collect errors with paths
  • 4. Act Accept the payload or return 400 / fix the data

Tools and Libraries

Use a maintained validator for your language, or try an online playground from the JSON Schema site’s Tools page. CodeToFun also has a JSON Validator for checking JSON text syntax.

Examples

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

Example 1 — Minimal type + required check

Educational validator for object schemas.

JSON
function miniValidate(instance, schema) {
  const errors = [];
  if (schema.type === "object") {
    if (instance === null || typeof instance !== "object" || Array.isArray(instance)) {
      errors.push("type: expected object");
      return errors;
    }
  }
  (schema.required || []).forEach(function (key) {
    if (!Object.prototype.hasOwnProperty.call(instance, key)) {
      errors.push("required: missing " + key);
    }
  });
  const props = schema.properties || {};
  Object.keys(props).forEach(function (key) {
    if (!Object.prototype.hasOwnProperty.call(instance, key)) return;
    const want = props[key].type;
    const val = instance[key];
    const got = val === null ? "null" : Array.isArray(val) ? "array" : typeof val;
    if (want === "integer") {
      if (!Number.isInteger(val)) errors.push(key + ": expected integer");
    } else if (want && got !== want) {
      errors.push(key + ": expected " + want);
    }
  });
  return errors;
}

const schema = {
  type: "object",
  required: ["name", "age"],
  properties: {
    name: { type: "string" },
    age: { type: "integer" }
  }
};

console.log(miniValidate({ name: "Ada", age: 36 }, schema));
console.log(miniValidate({ name: "Ada" }, schema));
console.log(miniValidate({ name: "Ada", age: "36" }, schema));
Try It Yourself

How It Works

  1. Empty error list means the educational check passed.
  2. Missing required keys and wrong types produce messages.
  3. Use a real library for full draft compliance.

Example 2 — Parse then validate

Invalid JSON never reaches the schema.

JSON
const text = '{"name":"Ada","age":36}';
const schema = {
  type: "object",
  required: ["name"],
  properties: { name: { type: "string" }, age: { type: "integer" } }
};

try {
  const instance = JSON.parse(text);
  const missing = (schema.required || []).filter(function (k) {
    return !(k in instance);
  });
  console.log("parsed OK");
  console.log("missing required:", missing);
} catch (err) {
  console.log(err.name + ": " + err.message);
}
Try It Yourself

How It Works

  1. Always parse first.
  2. Then apply schema checks.
  3. Syntax errors and schema errors are different problems.

Example 3 — Fail on bad payload

Catch a type mismatch after parse.

JSON
const instance = { productId: "1", productName: "Door", price: 12.5 };
const schema = {
  properties: {
    productId: { type: "integer" },
    productName: { type: "string" },
    price: { type: "number" }
  }
};

const id = instance.productId;
const ok = Number.isInteger(id);
console.log("productId integer?", ok);
if (!ok) console.log("validation failed for productId");
Try It Yourself

How It Works

  1. The JSON parsed fine — keys exist.
  2. Schema still rejects a string ID when integer is required.
  3. APIs usually return 400 with the keyword that failed.

Frequently Asked Questions

Typically a boolean valid flag plus error details listing which keyword failed and where in the instance.
No. JSON.parse only checks that text is valid JSON. A schema checks that the structure and types match your contract.
In JavaScript, Ajv is common. Python, Java, Go, and others have popular validators—pick one that supports your draft.
Yes with a JS validator library, or use an online tool. The Try It examples here show a tiny educational checker only.

Next: JSONP

Learn the legacy callback pattern around JSON.

JSONP 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