JSON Schema Types

Beginner
6 min read
Updated: Nov 2026
Reference

Definition and Usage

The type keyword is the first filter for an instance. Schema also defines integer (whole numbers) even though JSON text writes them as numbers.

Type Keyword Values

  • stringText in double quotes
  • numberAny numeric value (including fractions)
  • integerWhole numbers only (schema check)
  • objectJSON object { }
  • arrayJSON array [ ]
  • booleantrue or false
  • nullThe literal null

Multiple Types

Pass an array to allow more than one type—for example nullable strings:

nullable.json
{
  "type": ["string", "null"]
}

Examples

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

Example 1 — Match six JSON types

A helper that mirrors common type checks.

JSON
function jsonType(value) {
  if (value === null) return "null";
  if (Array.isArray(value)) return "array";
  return typeof value; // string, number, boolean, object
}

const samples = ["Ada", 36, true, null, { a: 1 }, [1, 2]];
samples.forEach(function (v) {
  console.log(JSON.stringify(v) + " → " + jsonType(v));
});
Try It Yourself

How It Works

  1. JSON has six value kinds in the data model.
  2. Schema integer is an extra check on numbers.
  3. Use type to declare which kind you expect.

Example 2 — integer vs number

Schema integer rejects fractional values.

JSON
function isInteger(value) {
  return typeof value === "number" && Number.isInteger(value);
}

function isNumber(value) {
  return typeof value === "number" && !Number.isNaN(value);
}

console.log("36 integer?", isInteger(36));
console.log("12.5 integer?", isInteger(12.5));
console.log("12.5 number?", isNumber(12.5));
Try It Yourself

How It Works

  1. number accepts 12.5.
  2. integer requires a whole number.
  3. Both are written as JSON numbers in the instance.

Example 3 — string or null

Accept either type with a type array.

JSON
const schema = { type: ["string", "null"] };

function matches(value, types) {
  const t = value === null ? "null" : typeof value;
  return types.indexOf(t) !== -1;
}

console.log(matches("Ada", schema.type));
console.log(matches(null, schema.type));
console.log(matches(1, schema.type));
Try It Yourself

How It Works

  1. A type array means “any one of these”.
  2. Nullable optional fields often use ["string", "null"].
  3. Numbers still fail this union.

Frequently Asked Questions

In JSON text, numbers are one type. JSON Schema adds integer as a validation type for whole numbers.
Use "type": ["string", "null"] so either value is accepted.
Store dates as strings (often ISO-8601) and optionally annotate with format: date-time.
type: "object" only requires an object. Use properties (and items for arrays) to describe the inside.

Next: Schema Keywords

Add enum, pattern, minLength, minItems, and more.

Schema Keywords 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