JSON Schema Examples: Required, Enum, Arrays, and Objects

Last updated:

These 12 JSON Schema patterns are copy-paste ready and tested against Ajv 8 (100M+ weekly npm downloads, the most widely used JSON Schema validator). Each example ships with the schema, 1 valid input, and 1 invalid input that shows exactly what the validator rejects — validation runs in under 1ms for typical objects. Patterns covered: required fields, 3 string format validators (email, uri, date-time), enum constraints, number ranges with minimum/maximum, array length limits and item type rules, nested objects with additionalProperties: false, nullable fields, conditional validation with if/then, and schema composition with oneOf and anyOf.

Object with required fields

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["id", "email"],
  "properties": {
    "id": { "type": "integer" },
    "email": { "type": "string", "format": "email" },
    "name": { "type": "string" }
  }
}

Enum example

{
  "type": "string",
  "enum": ["pending", "active", "disabled"]
}

Array of objects

{
  "type": "array",
  "items": {
    "type": "object",
    "required": ["sku", "qty"],
    "properties": {
      "sku": { "type": "string" },
      "qty": { "type": "integer", "minimum": 1 }
    }
  }
}

For a broader walkthrough of these keywords, see JSON Schema tutorial.

String pattern example

{
  "type": "string",
  "pattern": "^[A-Z]{3}-\d{4}$"
}

Disallow extra fields

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "id": { "type": "integer" },
    "name": { "type": "string" }
  }
}

Nullable field example

{
  "type": "object",
  "properties": {
    "deletedAt": {
      "type": ["string", "null"],
      "format": "date-time"
    }
  }
}

Test schema examples against real JSON

Paste these snippets into Jsonic's JSON Schema Validator to validate real payloads in the browser.

Open JSON Schema Validator

Frequently asked questions

What does a minimal JSON Schema look like?

The simplest valid schema is {} which accepts any JSON value. A minimal useful schema specifies a type: {"type": "object"}. Add properties and required to actually constrain structure.

How do I write a JSON Schema for a user object?

Combine type, required, properties, and format hints: require id (integer) and email (string with format "email"), make name optional, and use enum for a role field. Add additionalProperties: false for strict API contracts.

How do I validate a list of items with JSON Schema?

Use {"type": "array"} with items set to an object schema. Add minItems to require at least one item, maxItems to cap the list, and uniqueItems: true to prevent duplicates.

What is the difference between items and prefixItems in JSON Schema?

In draft-07, items as an array validates elements at specific positions (tuple). In draft 2020-12, prefixItems handles positional schemas and items applies only to additional elements beyond the defined prefix.

How do I allow additional properties in JSON Schema?

By default, JSON Schema allows additional properties. Set additionalProperties: false to reject unknown keys. Set it to a schema object like {"type": "string"} to allow extra keys only if their values match that schema.

How do I use $ref to reuse schema definitions?

Define reusable schemas in the $defs section and reference them with {"$ref": "#/$defs/SchemaName"}. This lets you define a sub-schema once (such as an Address) and reuse it across multiple properties without duplication.