# JSON Schema

**JSON Schema** 是一种用于描述 JSON 数据结构的词汇表与规范：一份 JSON Schema 文档对某个 JSON 值施加约束，描述从中提取信息的方式，并可描述如何与之交互。它定义了媒体类型“application/schema+json”，由 IETF 的 jsonschema 工作组以互联网草案的形式开发，JSON Schema 组织则发布带日期的草案，如当前的 Draft 2020-12。<sup>[1](https://datatracker.ietf.org/doc/draft-ietf-jsonschema-json-schema/)</sup><sup> • </sup><sup>[2](https://json-schema.org/specification)</sup>

| 关键事实 | 详情 |
|---|---|
| 当前版本 | Draft 2020-12；上一版本为 2019-09<sup>[2](https://json-schema.org/specification)</sup> |
| 发布情况 | Draft 2020-12 规范于 2022 年 6 月 16 日发布；作者为 Austin Wright、Henry Andrews、Ben Hutton 和 Greg Dennis<sup>[3](https://json-schema.org/draft/2020-12)</sup> |
| 规范组成部分 | 拆分为 Core（基础部分）与 Validation（验证关键字），另有一份 Relative JSON Pointers 规范<sup>[2](https://json-schema.org/specification)</sup> |
| 关键字行为 | 断言（布尔通过/失败）、注解（将信息附加到实例位置）和应用器（将子模式应用于实例的各个部分）<sup>[4](https://github.com/json-schema-org/json-schema-spec/blob/main/specs/jsonschema-core.md)</sup> |
| 方言声明 | `$schema` 关键字声明模式是为哪种方言编写的，必须出现在根层级<sup>[5](https://json-schema.org/understanding-json-schema/reference/schema)</sup> |
| 标准化状态 | 活跃的 IETF 互联网草案 draft-ietf-jsonschema-json-schema-04，预期状态为拟议标准<sup>[1](https://datatracker.ietf.org/doc/draft-ietf-jsonschema-json-schema/)</sup> |

## 模式的作用

JSON Schema 本身也是一份 JSON 文档。用模式评估实例，就是将模式的关键字作用于实例中的相应位置。规范定义了三种关键字行为。断言验证实例是否满足约束，产生布尔结果：满足约束为真，否则为假。注解将信息附加到实例位置，应用程序可以以任意方式使用这些信息。应用器将子模式应用于实例的各个部分，并合并其结果。<sup>[4](https://github.com/json-schema-org/json-schema-spec/blob/main/specs/jsonschema-core.md)</sup>

应用器关键字包括：用于就地组合子模式的 `allOf`、`anyOf`、`oneOf` 和 `not`；用于数组的 `prefixItems`、`items` 和 `contains`；以及用于对象的 `properties`、`additionalProperties`、`patternProperties` 和 `propertyNames`。未评估词汇表新增了 `unevaluatedItems` 和 `unevaluatedProperties`，用于约束未被其他任何关键字评估过的位置。<sup>[1](https://datatracker.ietf.org/doc/draft-ietf-jsonschema-json-schema/)</sup> 如果某个关键字缺失，它既不得产生为假的断言结果，也不得产生注解结果，其定义的行为也不得导致对任何其他模式的评估。<sup>[4](https://github.com/json-schema-org/json-schema-spec/blob/main/specs/jsonschema-core.md)</sup>

## 方言、词汇表与元模式

JSON Schema 的某个版本称为一种**方言**，代表可用于评估模式的关键字与语义的集合；每次发布都是一个新方言。`$schema` 关键字声明模式是为哪种方言编写的，其值同时也是某个模式的标识符，该模式可依据该方言验证文档。描述另一个模式的模式称为元模式。`$schema` 作用于整个文档，不适用于外部引用的文档，后者必须声明自己的 `$schema`。<sup>[5](https://json-schema.org/understanding-json-schema/reference/schema)</sup>

规范按词汇表组织，每个词汇表由一份说明其关键字语义的文档来规定。Draft 2020-12 为 Core、应用器、Validation、未评估、格式注解、格式断言、内容和元数据词汇表分别发布了各自的元模式。<sup>[3](https://json-schema.org/draft/2020-12)</sup> JSON Schema 元模式是自描述的：它能验证自身。<sup>[2](https://json-schema.org/specification)</sup>

## Draft 2020-12

Draft 2020-12 是对 draft 2019-09 的全面更新，回应了反馈与实现经验。其变更包括：<sup>[3](https://json-schema.org/draft/2020-12)</sup>

- 重新设计数组与元组关键字：`items` 和 `additionalItems` 被 `prefixItems` 和 `items` 取代。
- 动态引用：`$dynamicRef` 和 `$dynamicAnchor` 取代了较旧的 `$recursiveRef` 和 `$recursiveAnchor`。
- 规定了 `contains` 关键字如何影响 `unevaluatedItems`。
- 正则表达式现在预期支持 Unicode 字符，解决了此前草案中的不一致问题。
- 取消了模式媒体类型参数。
- 关于将模式打包为复合模式文档的指南。
- 将 `format` 词汇表拆分为 `format-annotation` 和 `format-assertion`。

最初的 2020-12 文档（draft-bhutton-json-schema-00 及对应的 Validation 草案）后来更新为 -01，未改变功能或元模式，仅修正错误和表述不清之处。<sup>[3](https://json-schema.org/draft/2020-12)</sup> 2020-12 各版本未产出 JSON Hyper-Schema 草案。<sup>[2](https://json-schema.org/specification)</sup>

## 标准化状态

该规范正在 IETF 中标准化。互联网草案 draft-ietf-jsonschema-json-schema 的第四版（2026 年 10 月）是一份活跃的工作组文档，预期状态为拟议标准；其编辑与作者为 Lisa M. Dusseault（Data Transfer Initiative）、Austin Wright 和 Henry H. Andrews。<sup>[1](https://datatracker.ietf.org/doc/draft-ietf-jsonschema-json-schema/)</sup> 由于该规范尚未达到 RFC 状态，采用它的标准机构指出该版本可能发生变化；例如，WIPO 标准 ST.97 要求 JSON Schema 使用值为 `"https://json-schema.org/draft/2020-12/schema"` 的 `$schema` 声明符合 2020-12。<sup>[7](https://www.wipo.int/edocs/mdocs/cws/en/cws_10/cws_10_6.pdf)</sup>

## 用途

**API 契约。** 借助 OpenAPI，可以规定完整的 HTTP API——涵盖其路径、操作、参数、响应、服务器和认证——并在其中嵌入 JSON Schema 来描述 API 操作收发的数据。行业机构已在此基础上发展：[ISO 20022](https://www.edgepedia.org/zh/iso-20022) 为其金融报文数据生成 Draft 2020-12 模式，WIPO 面向知识产权数据的 JSON 标准也基于同一草案。<sup>[6](https://www.iso20022.org/sites/default/files/media/file/ISO_20022_Generation_of_JSON_Schema_Draft_2020_12_for_ISO_20022_2013_10June2025.pdf)</sup><sup> • </sup><sup>[7](https://www.wipo.int/edocs/mdocs/cws/en/cws_10/cws_10_6.pdf)</sup>

**语言模型的结构化输出。** 对于结构化数据，约束解码的框架格局已趋同于 JSON Schema，且在多数情况下，给定模式会附带输出将遵守该模式的保证；一项基准研究使用官方 JSON Schema 测试套件评估了六个框架——Guidance、Outlines、Llamacpp、XGrammar、OpenAI 和 Gemini。<sup>[8](https://arxiv.org/abs/2501.10868)</sup>

## 实现与性能

多种语言都有验证器，JSON Schema 组织维护着一个实现页面，其中包含基于官方测试套件的 Go、JavaScript 和 PHP 验证器基准测试。<sup>[9](https://github.com/json-schema-org/json-schema-org.github.io/blob/main/implementations.md)</sup> 在一项 Node.js draft-06 基准测试中，`ajv` 每秒处理约 17,000 次测试运行，领先于 `@exodus/schemasafe` 的 11,760 次和 `is-my-json-valid` 的 8,757 次。<sup>[10](https://github.com/ebdrup/json-schema-benchmark)</sup> 一项 JVM 比较发现，对于较新的草案，`Skema`、`DevHarrel` 和 `SchemaFriend` 在功能与性能比较中领先，并观察到随着规范的演进，验证的一般成本似乎有所增加。<sup>[11](https://www.creekservice.org/articles/2023/11/14/json-validator-comparison.html)</sup>

形式化工作考察了语言本身的成本。对“现代”JSON Schema 的形式化——其相对于经典 JSON Schema 的决定性新增特性是依赖注解的验证和动态引用——发现使用这些特性的数据验证具有 PSPACE 复杂度，而经典 JSON Schema 验证预期处于 PTIME。<sup>[12](https://arxiv.org/abs/2307.10034)</sup> 在工程方面，Blaze 系统预先编译模式，报告验证速度约快 10 倍，同时严格遵循规范，并指出若干流行验证器在某些情况下会产生错误结果。<sup>[13](https://arxiv.org/abs/2503.02770)</sup>

## References

1. [draft-ietf-jsonschema-json-schema-04, IETF Datatracker](https://datatracker.ietf.org/doc/draft-ietf-jsonschema-json-schema/)
2. [Specification, json-schema.org](https://json-schema.org/specification)
3. [Draft 2020-12, json-schema.org](https://json-schema.org/draft/2020-12)
4. [JSON Schema Core specification (jsonschema-core.md), json-schema-org GitHub](https://github.com/json-schema-org/json-schema-spec/blob/main/specs/jsonschema-core.md)
5. [Dialect and vocabulary declaration, Understanding JSON Schema](https://json-schema.org/understanding-json-schema/reference/schema)
6. [Generation of JSON Schema Draft 2020-12 for ISO 20022:2013, ISO 20022](https://www.iso20022.org/sites/default/files/media/file/ISO_20022_Generation_of_JSON_Schema_Draft_2020_12_for_ISO_20022_2013_10June2025.pdf)
7. [CWS/10/6: Proposal for a New WIPO Standard on JSON, WIPO](https://www.wipo.int/edocs/mdocs/cws/en/cws_10/cws_10_6.pdf)
8. [Generating Structured Outputs from Language Models: Benchmark and Studies, arXiv](https://arxiv.org/abs/2501.10868)
9. [Implementations page, json-schema-org GitHub](https://github.com/json-schema-org/json-schema-org.github.io/blob/main/implementations.md)
10. [json-schema-benchmark (draft6), ebdrup GitHub](https://github.com/ebdrup/json-schema-benchmark)
11. [Comparison of JSON schema validator implementations, Creek Service](https://www.creekservice.org/articles/2023/11/14/json-validator-comparison.html)
12. [Validation of Modern JSON Schema: Formalization and Complexity, arXiv](https://arxiv.org/abs/2307.10034)
13. [Blaze: Compiling JSON Schema for 10x Faster Validation, arXiv](https://arxiv.org/abs/2503.02770)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Data formats and serialization

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/json-schema

English original: https://www.edgepedia.org/json-schema
