使用步驟
- 貼上 JSON 示例,或點「載入示例」。
- 設定根 Schema 的標題。
- 複製或下載 Schema,再按需補充 format、pattern、取值範圍等約束。
引數說明
- 根型別名稱
- 用作 Schema 的 title。
使用示例
{"id": 1, "email": null} 會生成 "id": {"type": "integer"}、"email": {"type": "null"},並且兩個鍵都在 required 中。
示例
兩條使用者資料,只有第一條有 email,manager 有時為 null:
[
{ "id": 1, "name": "Ada", "email": "ada@example.com", "tags": ["admin"], "manager": null },
{ "id": 2, "name": "Grace", "tags": [], "manager": { "id": 1 } }
]
根型別名填 User,生成的 Schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"email": { "type": "string" },
"tags": { "type": "array", "items": { "type": "string" } },
"manager": {
"anyOf": [
{ "type": "null" },
{ "type": "object", "properties": { "id": { "type": "integer" } }, "required": ["id"] }
]
}
},
"required": ["id", "name", "tags", "manager"]
}
}
第二條沒有 email,所以 email 不在 required 裡;manager 在樣例裡既有 null 又有物件,所以兩種都允許。樣例資料越全,生成的 Schema 越準,最好多貼幾條記錄。
常見用途
**校驗介面引數:**把 Schema 交給 Ajv(JavaScript)或 jsonschema(Python)這類校驗庫,不符合的請求直接拒絕。
**寫介面文件:**複製 properties 到 OpenAPI 的 components.schemas,作為文件的起點。
**編輯器提示:**VS Code、JetBrains 系列編輯器可以按 JSON Schema 給配置檔案補全欄位、標出錯誤。
常見問題
哪些鍵會被標為必填?
在所有示例物件中都出現的鍵。數組裡的物件鍵不一致時,只在部分元素中出現的鍵不會列入 required。
能用在 OpenAPI 裡嗎?
OpenAPI 3.1 使用 JSON Schema 2020-12,生成的 Schema 可以直接放在 components/schemas 下。OpenAPI 3.0 需要把 ["string", "null"] 這樣的型別陣列改成 nullable true。
能辨識電子郵件、日期時間等格式嗎?
不能,它只推斷型別。format、pattern、minimum 等關鍵字需要按需手動新增。
JSON 資料會上傳嗎?
不會。型別在瀏覽器中生成,包含真實資料的介面返回也不會離開你的裝置。
隱私說明
本工具完全在瀏覽器中執行,檔案和文字都在你的裝置上處理,不會上傳到我們的伺服器。