JSON Schema Composition

Beginner
6 min read
Updated: Nov 2026
Guides

Definition and Usage

Real schemas grow: nested objects, optional branches, and shared pieces. Composition keywords combine subschemas; $ref reuses them by URI.

Boolean Combination

  • allOf Must satisfy every subschema
  • anyOf Must satisfy at least one
  • oneOf Must satisfy exactly one
  • not Must fail the given subschema

$ref and External Schemas

Point a property at another schema document—common for shared address or geo types:

ref-example.json
{
  "warehouseLocation": {
    "description": "Coordinates of the warehouse",
    "$ref": "https://example.com/geographical-location.schema.json"
  }
}

Examples

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

Example 1 — Nested dimensions object

Embed an object schema inside properties.

JSON
const schema = {
  type: "object",
  properties: {
    dimensions: {
      type: "object",
      properties: {
        length: { type: "number" },
        width: { type: "number" },
        height: { type: "number" }
      },
      required: ["length", "width", "height"]
    }
  }
};

const instance = { dimensions: { length: 1, width: 2, height: 3 } };
const d = instance.dimensions;
const keys = schema.properties.dimensions.required;
console.log(keys.every(function (k) { return k in d; }));
console.log(d.length, d.width, d.height);
Try It Yourself

How It Works

  1. Nested properties describe inner objects.
  2. Inner required only applies inside that object.
  3. This matches the official dimensions example.

Example 2 — anyOf for string or number id

Accept either representation.

JSON
const schema = {
  anyOf: [
    { type: "string", minLength: 1 },
    { type: "integer" }
  ]
};

function anyOfOk(value) {
  const asString = typeof value === "string" && value.length >= 1;
  const asInt = typeof value === "number" && Number.isInteger(value);
  return asString || asInt;
}

console.log(anyOfOk("A-1"));
console.log(anyOfOk(42));
console.log(anyOfOk(true));
Try It Yourself

How It Works

  1. anyOf passes if any branch matches.
  2. oneOf would fail if two branches matched.
  3. Use this for flexible ID formats.

Example 3 — Simulate $ref lookup

Resolve a local map of schemas by $id.

JSON
const registry = {
  "https://example.com/geo.schema.json": {
    type: "object",
    required: ["latitude", "longitude"],
    properties: {
      latitude: { type: "number" },
      longitude: { type: "number" }
    }
  }
};

const productProperty = {
  $ref: "https://example.com/geo.schema.json"
};

const geo = registry[productProperty.$ref];
console.log(geo.required.join(", "));
console.log(geo.type);
Try It Yourself

How It Works

  1. $ref is a pointer to another schema URI.
  2. Validators load that document and apply it.
  3. Reuse keeps shared shapes in one place.

Frequently Asked Questions

The instance must be valid against every subschema in the allOf array.
anyOf needs at least one match. oneOf needs exactly one match.
$ref points at another schema by URI (often an $id) so you can reuse shared definitions.
Yes. Put another type: object with properties inside a property—nesting is normal.

Next: Validate Data

Check instances against schemas with a small validator.

Validate Data 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