📄 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)中使用
- 計算結果僅供參考,請以實際應用場景為準