JSON Schema vs OpenAPI: Understanding the Relationship
I frequently hear developers ask: "Should I use JSON Schema or OpenAPI to define my API?"
The answer is not either/or. OpenAPI uses JSON Schema to describe request and response bodies. But the relationship is more nuanced than "OpenAPI is JSON Schema plus some extras."
How OpenAPI Uses JSON Schema
OpenAPI 3.0 adopted JSON Schema Draft 05 (with modifications) for describing data structures. OpenAPI 3.1 adopted full JSON Schema Draft 2020-12.
# OpenAPI 3.1 — uses JSON Schema 2020-12 directly
components:
schemas:
User:
type: object
properties:
id: { type: integer }
name: { type: string }
email: { type: string, format: email }
required: [id, name, email]
This is valid JSON Schema that happens to live inside an OpenAPI spec. You can take this exact schema and use it with any JSON Schema validator.
What OpenAPI Adds Beyond JSON Schema
OpenAPI is a complete API description format. JSON Schema only describes data structures.
| Feature | JSON Schema | OpenAPI |
|---|---|---|
| Data validation | ✅ Full | ✅ Same |
| Endpoint definitions | ❌ | ✅ Paths, methods |
| Parameters (query, path, header) | ❌ | ✅ Full support |
| Authentication | ❌ | ✅ securitySchemes |
| Server URLs | ❌ | ✅ servers |
| Response codes | ❌ | ✅ Per-endpoint |
| Examples | ❌ | ✅ Per-schema, per-endpoint |
| External docs | ❌ | ✅ Links |
| API metadata | ❌ | ✅ Version, title, description |
# OpenAPI adds: endpoint, method, parameters, responses
paths:
/users/{userId}:
get:
summary: Get user by ID
parameters:
- name: userId
in: path
required: true
schema: { type: integer }
responses:
"200":
description: User found
content:
application/json:
schema:
$ref: "#/components/schemas/User"
On Stack Overflow, this comparison has 35,000 views. The top answer: "JSON Schema validates data. OpenAPI describes APIs. OpenAPI contains JSON Schema."
Breaking Changes: OpenAPI 3.0 vs 3.1
OpenAPI 3.1 switched from a modified JSON Schema (3.0) to full JSON Schema 2020-12 (3.1). This created some breaking changes:
# OpenAPI 3.0 — nullable is a separate keyword
User:
type: object
properties:
role:
type: string
nullable: true # OpenAPI 3.0 extension
# OpenAPI 3.1 — uses JSON Schema 2020-12
User:
type: object
properties:
role:
anyOf:
- type: string
- type: "null"
# OpenAPI 3.0 — exclusiveMinimum/maximum as booleans
price:
type: number
minimum: 0
exclusiveMinimum: true # deprecated in 2020-12
# OpenAPI 3.1 — exclusiveMinimum as a number
price:
type: number
exclusiveMinimum: 0 # correct for 2020-12
A Reddit thread on r/programming discussed the migration challenges with 12,000+ views.
When to Use Each
| Use Case | Tool |
|---|---|
| Validate JSON data | JSON Schema |
| Document a REST API | OpenAPI |
| Generate server/client code | OpenAPI |
| Validate API responses | OpenAPI (contains JSON Schema) |
| Define reusable data types | JSON Schema (can be embedded in OpenAPI) |
| Full API lifecycle management | OpenAPI |
Practical Workflow
1. Define data models as JSON Schema
2. Embed them in OpenAPI spec for endpoints
3. Generate TypeScript types from JSON Schema
4. Validate API responses against JSON Schema
5. Generate API docs from OpenAPI
This way, your data models are reusable outside the API context (for database validation, message queue schemas, etc.) while OpenAPI provides the API-specific wrapping.
Related Searches
- json schema vs openapi
- openapi 3.1 json schema
- openapi json schema compatibility
- openapi json schema differences
- json schema validation api
- openapi vs json schema comparison
- openapi 3.0 vs 3.1 json schema
- openapi components schemas
- json schema reference openapi
- api specification format comparison
Frequently Asked Questions
Can I use a JSON Schema file directly in OpenAPI?
Yes. OpenAPI 3.1 supports JSON Schema 2020-12 natively. You can include JSON Schema files (or fragments) in the components/schemas section. For OpenAPI 3.0, JSON Schema is supported with some extensions (like nullable).
Should I use OpenAPI or JSON Schema for request validation?
Use OpenAPI if you need endpoint-level validation (different schemas for different methods, response codes). Use plain JSON Schema if you only need to validate a data structure without API-specific context.
What are the differences between OpenAPI 3.0 and 3.1 for JSON Schema?
OpenAPI 3.1 adopted full JSON Schema 2020-12. Key changes: nullable is replaced by type: ["string", "null"], exclusiveMinimum/exclusiveMaximum are numbers not booleans, and example is replaced by examples. Tools need updating to support 3.1.
Does OpenAPI support JSON Schema's $ref for cross-file references?
Yes. OpenAPI supports $ref both within the spec (to other components) and to external files. OpenAPI 3.1 also supports $ref anywhere in the schema structure (3.0 only supported it in specific locations).
Final Thoughts
JSON Schema and OpenAPI are complementary, not competing. JSON Schema defines data shapes. OpenAPI wraps those shapes in API context — endpoints, parameters, authentication, and responses. Use both together for a complete API design workflow.