JSON Schema 验证器
在浏览器中即时校验 JSON 是否符合任意 JSON Schema。支持 Draft 2020-12、2019-09 与 Draft-07,错误信息精确定位到 JSON Pointer 路径。100% 隐私保护,零上传、零账号、永久免费。
什么是 JSON Schema 验证器?
JSON Schema 验证器接收两份 JSON 文档 —— 一份数据、一份 schema —— 并报告数据是否符合 schema 的契约。Schema 用一组固定词汇(type、properties、required、items、enum、oneOf、allOf、$ref、format)声明字段类型、必需键、值范围、允许的 enum 值、正则 pattern 与结构规则。校验器并行遍历两份文档,输出零或多条错误,每条都精确定位到数据内部的 JSON Pointer 路径。
校验在运行时执行,处于不可信输入与你的代码之间的边界。TypeScript 类型在编译期就被擦除,对来自 webhook、第三方 API 或用户粘贴的 JSON 无能为力 —— 这正是 JSON Schema 要填补的空白。把它和 TypeScript(或 Python 中的 Pydantic)配合,就能在代码内部获得编译期保证、在边界获得运行时保证。
Draft 2020-12 是当前规范,2026 年新项目应当首选。早期 draft(2019-09、Draft-07、Draft-06、Draft-04)仍存活于历史代码中 —— Draft-07 在 Helm chart、VS Code settings 与旧版 Ajv 配置中仍十分常见。OpenAPI 3.1 原生使用 Draft 2020-12;OpenAPI 3.0 使用的是 Draft 4 子集。
本工具完全在浏览器中运行。你的 JSON、schema 与校验输出都不会离开你的机器 —— 可安全用于内部 API 契约与敏感 payload。内部 $ref 指针自动解析;外部 HTTP ref 出于隐私考虑被有意禁用。
配合相邻的 JSON 工具?粘贴前用 JSON 格式化工具 整理 JSON;用 JSON Diff 对比 比较两份 JSON;用 JSON 转 YAML 与 YAML 转 JSON 做格式互转。要看 Node、Python 与浏览器端的端到端校验示例,请阅读我们的 JSON Schema 校验指南。
// A 5-line schema that catches three real bugs
const schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": { "type": "integer", "minimum": 1 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0, "maximum": 150 }
},
"required": ["id", "email"],
"additionalProperties": false
};
// Three bugs the schema catches:
const bad = { "id": "42", "age": 200 };
// /id → type: expected integer, got string
// /email → required: missing
// /age → maximum: 200 > 150
// In Node: new Ajv().compile(schema)(bad) // false; ajv.errors has the paths
// In Python: jsonschema.validate(bad, schema)
// In the browser: this tool — same errors, same paths, no install 核心特性
多 Draft 支持
Draft 2020-12(默认)、2019-09 与 Draft-07。从 schema 的 $schema URI 自动识别 draft;未声明时使用下拉框作为回退。
路径精确的错误信息
每条错误都包含 JSON Pointer(如 /user/email/0)、失败的 keyword(type、required、pattern)与一句话说明。点击即可跳转到对应位置。
实时校验
输入时即时校验。错误实时更新,调试 schema 或数据无需反复点击 Validate 按钮。
format Keyword 全覆盖
email、uri、uuid、date、date-time、ipv4、ipv6、hostname、regex —— 全部使用经过实战检验的 pattern 校验。
100% 浏览器端
输入永不离开你的机器。零上传、不对粘贴内容做分析、不把 JSON 写入 localStorage。可安全用于内部契约与敏感 payload。
示例 schema 一键加载
可加载预设(注册表单、webhook 信封、配置文件、订单数组),五秒内进入可工作的校验状态。
示例
合法对象 — required + 类型校验
{
"id": 42,
"email": "alice@example.com",
"age": 30
} {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": { "type": "integer", "minimum": 1 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0, "maximum": 150 }
},
"required": ["id", "email"],
"additionalProperties": false
} Schema 将 id 与 email 声明为 required 并锁定类型。上方数据满足全部约束,校验通过。试着删掉 email 或把 id 改成字符串,就能看到精确路径的错误提示。
不合法 — 缺失 required + 类型错误
{
"id": "42",
"age": 200
} {
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "maximum": 150 }
},
"required": ["id", "email"]
} 三处错误:/id 是字符串而非 integer、/email 缺失、/age(200)超过 maximum 150。每条错误都附带 JSON Pointer 路径,无需猜测就能修数据 —— 或者放宽 schema。
判别联合 — oneOf 配合 const
{
"type": "order.created",
"data": { "orderId": "ORD-001234", "totalUsd": 49.99 }
} {
"oneOf": [
{
"properties": {
"type": { "const": "order.created" },
"data": {
"type": "object",
"properties": {
"orderId": { "type": "string", "pattern": "^ORD-[0-9]{6}$" },
"totalUsd": { "type": "number", "minimum": 0 }
},
"required": ["orderId", "totalUsd"]
}
},
"required": ["type", "data"]
},
{
"properties": {
"type": { "const": "order.refunded" },
"data": { "type": "object", "required": ["refundId"] }
},
"required": ["type", "data"]
}
]
} Webhook 信封校验。第一个 oneOf 分支因 type 为 "order.created" 而命中。把 type 改为 "order.refunded" 或破坏 orderId 的 pattern,可看到 oneOf 如何按分支汇报失败。
对象数组 — items + uniqueItems
[
{ "sku": "A1", "qty": 3 },
{ "sku": "B2", "qty": 5 },
{ "sku": "A1", "qty": 3 }
] {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "object",
"properties": {
"sku": { "type": "string", "pattern": "^[A-Z][0-9]+$" },
"qty": { "type": "integer", "minimum": 1 }
},
"required": ["sku", "qty"]
}
} 两条记录字节级一致,uniqueItems 触发。索引 0 与 2 重复 —— 校验器会在数组根上报告。适合捕获购物车重复行与合并冲突的发货请求。
使用方法
- 1
粘贴 JSON Schema
把 schema 粘贴到右栏。校验器会从 $schema URI 自动识别 draft;缺失时从工具栏选择 Draft 2020-12、2019-09 或 Draft-07。
- 2
粘贴 JSON 数据
把要校验的 JSON 文档粘贴到左栏。输入时实时校验;超过 200 KB 的大输入会切换到手动 Validate 按钮,保证输入流畅。
- 3
阅读错误列表
每条错误都有 JSON Pointer 路径、keyword 与一句话说明。点击错误跳转到数据中的对应位置,修好后看错误数量实时下降。
常见的 JSON Schema 陷阱
type:integer 与 number 之分
JSON Schema 把 1.0 视为 number 而非 integer。如果你的契约写了 integer,校验器会拒绝 1.0 —— 即便大多数语言认为它等于 1。除非真的需要 integer,否则用 number。
{ "qty": 1.0 } schema: { "type": "integer" } → error: not an integer { "qty": 1 } schema: { "type": "integer" } → valid required 写在错误的嵌套层级
required 必须与 properties 同级,不能写在某个 property 内部。写在 property 声明里的 required 数组会被静默忽略 —— 校验器永远不会强制它。
{ "properties": { "name": { "type": "string", "required": true } } } { "properties": { "name": { "type": "string" } }, "required": ["name"] } additionalProperties 默认是 true
不写 additionalProperties: false,schema 默认开放 —— 任何额外键都会通过。这是 schema「什么都接受」最常见的原因。
{ "properties": { "id": { "type": "integer" } } } accepts: { "id": 1, "foo": "bar", "x": null } { "properties": { "id": { "type": "integer" } }, "additionalProperties": false } OpenAPI 3.0 nullable 与 Draft 2020-12 type 数组
OpenAPI 3.0 用 nullable: true;Draft 2020-12 用 type: ["string", "null"]。混用会得到看起来正确、实际上从不允许 null 的 schema。
{ "type": "string", "nullable": true } in 2020-12: nullable is just an unknown keyword { "type": ["string", "null"] } in 2020-12: explicitly allows null pattern 没有锚点
JSON Schema 正则默认是部分匹配 —— pattern: "^[A-Z]+$" 锚定整串,但 pattern: "[A-Z]+" 只要字符串里出现一个大写字母就会通过。
pattern: "[A-Z]+" accepts: "helloX" (because X matches)
pattern: "^[A-Z]+$" accepts only: "HELLO"
用了 oneOf 实则想表达 anyOf
oneOf 要求恰好一个分支匹配。如果两个分支都接受同一形状,oneOf 会在 anyOf 本可通过的数据上失败 —— 错误信息也很费解("matches more than one")。
oneOf: [ { type: "string" }, { type: "string", maxLength: 10 } ] on: "hi" → error: matches both anyOf: [ { type: "string" }, { type: "string", maxLength: 10 } ] on: "hi" → valid 常见使用场景
- API 请求校验
- 上线前粘贴请求体与端点 schema,提前发现测试覆盖不到的 400 响应 —— 缺失的必需字段、错误的类型、超范围的数值。
- Webhook Payload 验证
- 厂商发来的 payload 被你的 handler 拒绝?用你的 schema 校验实际 payload,再用厂商发布的 schema 校验一次,二者的差就是 bug 所在。
- 配置文件 Lint
- package.json、tsconfig.json、helm values.yaml —— 每种配置文件都有公开 schema。粘贴 schema、粘贴配置、找到拼写错误,跳过反复试错。
- OpenAPI 组件测试
- 从 OpenAPI 3.1 文档中拿出一个 schema 组件,粘到这里校验示例 payload。比起 mock 服务器更快、确定性更高,不需要任何 SDK。
- 表单提交预演
- 在接前端校验之前粘一份示例表单 payload。确认 schema 拒绝你预期会拒绝的、接受你预期会接受的,再把同一份 schema 部署到客户端与服务端。
- 数据管道契约检查
- ETL 输出漂移了?粘一行示例数据与下游 schema,定位是哪个生产者改动、哪些键破坏了契约 —— 别等管道把 1 万条记录全部重试一遍。
技术细节
- Draft 2020-12 兼容
- 实现已发布的 Draft 2020-12 规范 —— keyword、类型系统、format 词汇表。与 Ajv 8.x 及 ajv-formats 输出交叉对比验证。
- JSON Pointer 错误路径
- 错误使用 RFC 6901 JSON Pointer(/user/email/0)。每条 keyword 失败都精确指向数据中唯一可解析的位置 —— 没有歧义、不需要字符串搜索。
- 内部 $ref 解析
- 解析单文档内的 $ref 指针(#/$defs/foo、#/properties/bar)。检测并报告引用环。出于隐私考虑禁用外部 HTTP $ref。
最佳实践
- 始终设置 additionalProperties: false
- 在输入契约(请求体、配置文件、队列消息)上,未声明的键通常都是 bug —— 拼写错误、误加字段或攻击者探测。默认拒绝它们。
- 用 $defs 复用子 Schema
- 同一形状内联两遍,迟早会漂移。把共享定义放进 $defs,用 $ref 引用 —— 单一真相来源,一次修改全部生效。
- 在业务逻辑之前先校验
- JSON.parse 之后立刻跑 schema 校验,再去碰解析后的结构。类型收窄、默认值填充、持久化都假设契约成立 —— 先确保它真的成立。
常见问题
什么是 JSON Schema 校验?
这个校验器支持哪些 JSON Schema Draft 版本?
如何用 schema 校验 JSON?
JSON Schema 校验和 JSON 语法校验有什么区别?
为什么看起来正确的 JSON 却被 schema 拒绝?
支持 $ref 与远程 schema 引用吗?
additionalProperties: false 是做什么的?
如何在 Node.js 或 Python 中用 schema 校验 JSON?
oneOf、anyOf 与 allOf 有什么区别?
支持 OpenAPI schema 吗?
为什么校验器说我的 JSON Schema 本身不合法?
工具会把我的 JSON 或 schema 发送到服务器吗?
可以校验 JSON Lines(NDJSON)或多份文档吗?
相关工具
查看所有工具 →Base64 解码与编码工具
编码和格式化
免费在线 Base64 解码编码工具。实时转换,支持中文和 Emoji,100% 浏览器端运行,数据不离开设备,无需注册。
JSON Diff 对比
编码和格式化
在浏览器中即时对比两份 JSON。Side-by-Side 高亮 + RFC 6902 JSON Patch 输出,一键忽略 timestamps、IDs 等噪音字段。100% 隐私保护,零上传。
JSON 格式化与验证工具
编码和格式化
在浏览器中即时格式化、验证和美化 JSON。免费在线工具,支持语法验证、错误检测、压缩和一键复制,100% 隐私保护。
JSON 转 YAML 转换器
编码和格式化
粘贴 JSON 即时获得 YAML,浏览器端实时转换。支持 Kubernetes、Docker Compose 和 Terraform,提供 2/4 空格缩进、Norway 安全自动引号,100% 隐私保护,数据不离开设备。
二维码生成器 — URL / WiFi / vCard / Email / SMS / 位置
编码和格式化
免费二维码生成器,支持 URL、WiFi、vCard、邮件、短信、位置等 7 种类型。直接生成静态 QR Code,永不过期,无需注册,100% 浏览器本地,零上传。SVG 与 PNG 任选。
URL 编码解码在线工具 — 内置 URL 结构解析
编码和格式化
粘贴 URL 即时编码或解码,实时显示结果。内置 URL 解析器将协议、域名、路径、查询参数拆解为可编辑字段。支持 encodeURI 和 encodeURIComponent 双模式切换,检测双重编码问题。纯浏览器端运行,数据不离开设备。