Back to blog
Schema9 min read

JSON Schema Examples: 8 Patterns You Can Copy

Practical JSON Schema examples: required fields, types, string formats and patterns, number ranges, enums, nested objects, arrays with item rules, and conditional validation — each explained.

By Saifur Rahman Shochib

JSON Schema is the standard way to describe and validate the shape of JSON — required fields, types, formats, and constraints. The spec is large, but most real schemas reuse a handful of patterns. This guide walks through practical, copyable JSON Schema examples, from a basic object to conditional rules, so you can validate API payloads and config with confidence.

A basic object with required fields

Every schema starts with a type. For an object, you list its properties and which ones are required: type set to object, a properties map describing each field, and a required array naming the must-have keys. Validation fails if a required key is missing or if a value has the wrong type. This single pattern covers the majority of everyday validation — confirming a payload has the fields your code expects.

Object with typed, required fieldsjson
{
  "type": "object",
  "properties": {
    "id":   { "type": "integer" },
    "name": { "type": "string" }
  },
  "required": ["id", "name"]
}

Types and the type keyword

  • string, number, integer, boolean, object, array, and null are the available types.
  • A property can allow multiple types with an array, such as type: [string, null] for a nullable field.
  • integer is stricter than number — it rejects values with a fractional part.
  • additionalProperties: false rejects any key not listed in properties, locking the object down.

String formats and patterns

Strings can be constrained beyond just being text. The format keyword validates well-known shapes like email, date-time, uri, and uuid. For custom rules, pattern applies a regular expression — for example requiring a value to match a phone or product-code format. minLength and maxLength bound the length. These turn a loose string field into a precise contract.

String format and lengthjson
{
  "type": "string",
  "format": "email",
  "minLength": 5
}

Numbers, ranges, and enums

Numeric fields use minimum and maximum (and the exclusive variants) to bound a range, and multipleOf to require, say, multiples of 5. For a fixed set of allowed values, the enum keyword lists them: a status field might be constrained to the enum active, archived, and deleted, rejecting anything else. Enums are the cleanest way to validate a closed set of options.

Enum for a closed set; range for numbersjson
{
  "status": { "enum": ["active", "archived", "deleted"] },
  "age":    { "type": "integer", "minimum": 0, "maximum": 120 }
}

Nested objects and arrays

Schemas nest just like the data. A property whose type is object gets its own properties and required list, validating sub-structures to any depth. For arrays, the items keyword describes what each element must look like — for instance an array of objects each requiring an id. minItems, maxItems, and uniqueItems add list-level constraints like non-empty and no duplicates.

An array of objects with item rulesjson
{
  "type": "array",
  "minItems": 1,
  "items": {
    "type": "object",
    "required": ["id"],
    "properties": { "id": { "type": "integer" } }
  }
}

Conditional validation

Advanced schemas express rules like 'if type is business, then a tax ID is required' using if/then/else, or combine sub-schemas with allOf, anyOf, and oneOf. These let one schema validate several related shapes — for example a payment object that requires different fields depending on its method. Reach for them when a flat list of properties cannot capture the real rules.

Generating and testing schemas

You do not have to write schemas by hand. A JSON Schema Generator infers a starting schema from a sample document, which you then tighten with formats, ranges, and required fields. A JSON Schema Validator checks real payloads against your schema and reports exactly which rule failed, so you can iterate quickly toward a contract that matches your data.

Frequently asked questions

How do I make a field required in JSON Schema?

List the field name in the schema's required array, which sits alongside properties at the object level. Validation then fails if that key is missing. Note that required controls presence, not type — the type is set per property in properties.

How do I validate an array in JSON Schema?

Set type to array and use the items keyword to describe each element's schema. Add minItems, maxItems, and uniqueItems for list-level rules like non-empty or no duplicates.

How do I restrict a field to specific values?

Use the enum keyword with an array of allowed values, for example enum: [active, archived, deleted]. Any value outside the list fails validation, which is ideal for status fields and fixed option sets.

Can JSON Schema validate email or date formats?

Yes. The format keyword validates known shapes like email, date-time, uri, and uuid, and the pattern keyword applies a custom regular expression for anything else.

How do I write a JSON Schema quickly?

Generate a draft from a sample with a JSON Schema generator, then refine it by adding required fields, formats, and ranges. Validate real payloads against it to confirm the rules match your data.

Related tools

Try these tools

Keep reading

Related guides