liveBook · Manning

JSON Schema

As explained in The Design of Web APIs, Second Edition

A JSON Schema format is an annotation identifying an expected semantic representation for a value of a specified JSON data type.

JSON Schema is an independent format for validating and annotating JSON or YAML data. In OpenAPI, it gives data models machine-readable meaning for API design, documentation, mocking, testing, and code generation. section 7.1.1 section 19.3

Use reusable schemas under components.schemas for resource models, then reference or combine them in operation inputs and outputs. This replaces spreadsheet-based modeling with an artifact that tools can read, validate, and reuse. section 7.1 section 7.8

Build a schema from the data shape outward:

  • Set type to object, array, string, number, integer, or boolean. section 7.4.1 section 7.8
  • Describe object members under properties, giving each property its own schema. section 7.4.3
  • Mark mandatory object members by listing their names in required. section 7.8
  • Describe arrays with items, whose value is the schema of each element. section 7.4.5
  • Add descriptions, formats, ranges, lengths, patterns, enumerations, defaults, and examples when greater precision helps consumers or tools. section 7.4.3 section 19.3

A Product schema in OpenAPI YAML

components: schemas: Product: type: object properties: productReference: type: integer description: Unique identifier price: type: number description: Price in USD dateAdded: type: string format: date keywords: type: array items: type: string The same schema can support a reusable resource model, while an operation parameter can use an inline integer schema or reference part of the model. Use $ref to avoid duplicated request and response definitions. section 7.4.3 section 7.4.5 section 7.6.1 section 7.8

Start with essential types and descriptions while modeling, then add precision in a later pass. This keeps early design focused on identifying data while enabling documentation, testing, mocking, and code generation to use stronger constraints. section 7.1.3 section 19.3

Schemas also describe non-body parameters and response headers. Applying precise schemas to error responses can constrain fields and patterns, helping prevent improper formats and sensitive information leaks. section 7.6.1 section 12.10.5

Figure 7.1 Using a spreadsheet for the data models of our API was only to simplify our learning. We can describe data models with JSON Schema in our OpenAPI document once we start designing them.
figure

Use this figure to connect spreadsheet-based data modeling with its replacement: JSON Schema embedded in the OpenAPI document. section 7.1

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains core types, properties, arrays, required members, references, formats, and precision keywords, while deferring advanced features and optimization.
Examples
Uses Product models, atomic properties, arrays of strings, path parameters, response headers, and constrained unexpected-error responses.
What it assumes
Assumes familiarity with OpenAPI authoring and API operations, while teaching JSON Schema fundamentals from object and atomic types upward.
What you can do after
Apply it via OpenAPI to build reusable resource models and precise operation data contracts.
How it connects
Connects JSON Schema with OpenAPI, API design, data modeling, validation, documentation, mocking, testing, code generation, and security.

Also explained in

Compare how 4 books explain JSON Schema →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.