📄 JSON Schema Generator (from sample)
📐 工作原理与说明
由样例数据递归推断 Schema:null 得 type=「null」;数组得 type=「array」,非空时对每个元素递归推断后合并为 items;字符串得 type=「string」,并按内容依次尝试识别 format——邮箱、URI、日期时间、日期、IPv4、IPv6、UUID;数字按 Number.isInteger 区分 integer 与 number;布尔得 boolean;对象逐键递归放入 properties。勾选项决定附加属性:required 收集必填键、examples 记录样例值(数组取前两项)、descriptions 生成说明、title 生成标题、minMax 为数组加 minItems=0、为字符串加 minLength=0 与 maxLength=当前长度、为数字在 ≥ 0 时加 minimum=0;负数不添加下限,避免把原始样本自身排除。字段数按 properties 与 items 递归累加,大小取序列化后的字符数;顶部 $schema 按所选版本写入对应草案地址
由示例 JSON 推断生成 JSON Schema。
- 推断:根据出现的键、值类型与出现频率生成 type、properties 与 required。
- required:本工具按单个样本推断,对象中的非 null 键均列入必填;null 字段不列入(避免「必填且只能为 null」),数组元素的多个对象只取共有键。多份样本需人工合并后再调整。
- 数组:元素类型由所有元素推断,混合类型可用 anyOf/oneOf 表达。
- 局限:结果为草稿,格式约束(format、pattern、数值范围)需人工补充。
Generate a JSON Schema from sample JSON in your browser — infer types and structure automatically.
⏳ 等待输入
字段数: 0
大小: 0
📋 Schema
🌳 结构树
The output area is editable — paste or edit a schema there, then click "✅ Validate" for a structural check (common structure of draft-04/06/07 and 2020-12; boolean schemas are valid too).
等待生成...
📚 深度解析:JSON Schema 生成器
💡 常见使用场景
- 由样例 JSON 推断 Schema 用于校验。
- 给接口先补一份契约:从真实响应样本生成初版 Schema,团队评审后作为前后端联调依据。
生成
样例 `{"name":"Tom","age":20}` → Schema 标注 name:string、age:integer。
❓ 常见问题(FAQ)
- 推断的 Schema 完整吗?
- 基于样例,极端情况需人工补充约束。
- 为什么推断出来的类型不准?
- 因为只有一份样本:字段恰好为 null 会被推断成 null 类型,数字 1 无法区分整数还是浮点,空数组也无法得知元素类型。改进办法是用多份样本合并推断,之后再人工补全 required、format 与取值范围等业务约束。
Formula & Standard References
| Item | Formula / Note | Source / Standard |
|---|---|---|
| Type inference | infer type: object, array, string, number, boolean, null | JSON Schema 2020-12 |
| Required fields | keys present in the sample go into the required array | JSON Schema 2020-12 |
| Numeric constraints | optional minimum, maximum and multipleOf assertions | JSON Schema 2020-12 |
⚠️ 使用说明与注意事项
- 本工具纯前端运行,数据不会上传到服务器
- 建议在主流浏览器(Chrome/Safari/Firefox/Edge)中使用
- 计算结果仅供参考,请以实际应用场景为准