在本頁中,您將找到說明不同使用案例的範例,以幫助您充分利用 JSON Schema。這些範例涵蓋各種情境,每個範例都附帶 JSON 資料和說明,展示 JSON Schema 如何應用於各種領域。您可以修改這些範例以符合您的特定需求,因為這只是您使用 JSON Schema 的眾多方式之一。
地址
一個表示地址的 schema,其中包含不同地址組件的可選屬性,它強制規定 locality
、region
和 countryName
是必填項,並且如果提供了 postOfficeBox
或 extendedAddress
,則也必須提供 streetAddress
。

schema
{ "$id": "https://example.com/address.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "description": "An address similar to http://microformats.org/wiki/h-card", "type": "object", "properties": { "postOfficeBox": { "type": "string" }, "extendedAddress": { "type": "string" }, "streetAddress": { "type": "string" }, "locality": { "type": "string" }, "region": { "type": "string" }, "postalCode": { "type": "string" }, "countryName": { "type": "string" } }, "required": [ "locality", "region", "countryName" ], "dependentRequired": { "postOfficeBox": [ "streetAddress" ], "extendedAddress": [ "streetAddress" ] }}
資料
{ "postOfficeBox": "123", "streetAddress": "456 Main St", "locality": "Cityville", "region": "State", "postalCode": "12345", "countryName": "Country"}
部落格文章
一個代表部落格文章的結構描述(schema),包含像是 title
、content
、publishedDate
、author
和 tags
等屬性。

schema
{ "$id": "https://example.com/blog-post.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "description": "A representation of a blog post", "type": "object", "required": ["title", "content", "author"], "properties": { "title": { "type": "string" }, "content": { "type": "string" }, "publishedDate": { "type": "string", "format": "date-time" }, "author": { "$ref": "https://example.com/user-profile.schema.json" }, "tags": { "type": "array", "items": { "type": "string" } } }}
資料
{ "title": "新部落格文章", "content": "這是部落格文章的內容...", "publishedDate": "2023-08-25T15:00:00Z", "author": { "username": "authoruser", "email": "[email protected]" }, "tags": ["技術", "程式設計"]} 日曆
一個代表日曆中事件的綱要,包括 startDate
、endDate
、summary
、location
和 recurrenceDate
詳細資訊等屬性。 geo
屬性是一個參考 ($ref
),指向在不同位置定義的另一個綱要,該綱要表示具有緯度和經度值的地理位置。

schema
{ "$id": "https://example.com/calendar.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "description": "A representation of an event", "type": "object", "required": [ "dtstart", "summary" ], "properties": { "startDate": { "type": "string", "description": "Event starting time" }, "endDate": { "type": "string", "description": "Event ending time" }, "summary": { "type": "string" }, "location": { "type": "string" }, "url": { "type": "string" }, "duration": { "type": "string", "description": "Event duration" }, "recurrenceDate": { "type": "string", "description": "Recurrence date" }, "recurrenceDule": { "type": "string", "description": "Recurrence rule" }, "category": { "type": "string" }, "description": { "type": "string" }, "geo": { "$ref": "https://example.com/geographical-location.schema.json" } }}
資料
{ "startDate": "2023-08-25T10:00:00Z", "endDate": "2023-08-25T12:00:00Z", "summary": "會議簡報", "location": "會議中心", "recurrenceDule": "FREQ=DAILY;COUNT=5"}
裝置類型
此綱要代表電子裝置,其中 deviceType
屬性決定裝置的類別,例如 smartphone
(智慧型手機)或 laptop
(筆記型電腦)。它使用 oneOf
關鍵字,根據 deviceType
屬性動態參照綱要。這種彈性的綱要結構允許資料根據指定的 deviceType 符合適當的裝置綱要,使其易於描述具有獨特特徵的不同電子裝置。當 deviceType
設定為 smartphone
時,綱要會強制執行智慧型手機的特定屬性;當 deviceType
設定為 laptop
時,則會強制執行筆記型電腦的特定屬性。
{ "$id": "https://example.com/device.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "type": "object", "properties": { "deviceType": { "type": "string" } }, "required": ["deviceType"], "oneOf": [ { "properties": { "deviceType": { "const": "smartphone" } }, "$ref": "https://example.com/smartphone.schema.json" }, { "properties": { "deviceType": { "const": "laptop" } }, "$ref": "https://example.com/laptop.schema.json" } ]}
{ "$id": "https://example.com/smartphone.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "type": "object", "properties": { "brand": { "type": "string" }, "model": { "type": "string" }, "screenSize": { "type": "number" } }, "required": ["brand", "model", "screenSize"]}
{ "$id": "https://example.com/laptop.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "type": "object", "properties": { "brand": { "type": "string" }, "model": { "type": "string" }, "processor": { "type": "string" }, "ramSize": { "type": "number" } }, "required": ["brand", "model", "processor", "ramSize"]}
資料
{ "deviceType": "smartphone", "brand": "Samsung", "model": "Galaxy S21", "screenSize": 6.2}
電子商務系統
代表電子商務系統的綱要,其中在 product
(產品)和 order
(訂單)綱要的定義中使用 $anchor
來定義錨點:分別是 ProductSchema
和 OrderSchema
。

schema
{ "$id": "https://example.com/ecommerce.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "$defs": { "product": { "$anchor": "ProductSchema", "type": "object", "properties": { "name": { "type": "string" }, "price": { "type": "number", "minimum": 0 } } }, "order": { "$anchor": "OrderSchema", "type": "object", "properties": { "orderId": { "type": "string" }, "items": { "type": "array", "items": { "$ref": "#ProductSchema" } } } } }}
資料
{ "order": { "orderId": "ORD123", "items": [ { "name": "Product A", "price": 50 }, { "name": "Product B", "price": 30 } ] }}
地理位置
一個表示地理座標的結構,包含指定範圍內的 latitude
(緯度)和 longitude
(經度)值。

schema
{ "$id": "https://example.com/geographical-location.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "title": "Longitude and Latitude Values", "description": "A geographical coordinate.", "required": [ "latitude", "longitude" ], "type": "object", "properties": { "latitude": { "type": "number", "minimum": -90, "maximum": 90 }, "longitude": { "type": "number", "minimum": -180, "maximum": 180 } }}
資料
{ "latitude": 48.858093, "longitude": 2.294694}
健康記錄
一個表示健康記錄的結構,包含 patientName
(病人姓名)、dateOfBirth
(出生日期)、bloodType
(血型)、allergies
(過敏)、conditions
(狀況)、medications
(藥物)和 emergencyContact
(緊急聯絡人)。

schema
{ "$id": "https://example.com/health-record.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "description": "Schema for representing a health record", "type": "object", "required": ["patientName", "dateOfBirth", "bloodType"], "properties": { "patientName": { "type": "string" }, "dateOfBirth": { "type": "string", "format": "date" }, "bloodType": { "type": "string" }, "allergies": { "type": "array", "items": { "type": "string" } }, "conditions": { "type": "array", "items": { "type": "string" } }, "medications": { "type": "array", "items": { "type": "string" } }, "emergencyContact": { "$ref": "https://example.com/user-profile.schema.json" } }}
資料
{ "patientName": "Jane Doe", "dateOfBirth": "1985-02-15", "bloodType": "A+", "allergies": ["Pollen", "Penicillin"], "conditions": ["Hypertension", "Diabetes"], "medications": ["Lisinopril", "Metformin"], "emergencyContact": { "username": "emergencyuser", "email": "[email protected]" }} 職位發布
這是一個代表職位發布的結構描述,包含像是 title
(職稱)、company
(公司)、location
(地點)、description
(描述)、employmentType
(雇用類型)、salary
(薪資)和 applicationDeadline
(申請截止日期)等屬性。它也使用 $anchor
關鍵字來定義錨點。

schema
{ "$id": "https://example.com/job-posting.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "description": "A representation of a job posting", "type": "object", "required": ["title", "company", "location", "description"], "properties": { "title": { "type": "string" }, "company": { "type": "string" }, "location": { "type": "string" }, "description": { "type": "string" }, "employmentType": { "type": "string" }, "salary": { "type": "number", "minimum": 0 }, "applicationDeadline": { "type": "string", "format": "date" } }}
資料
{ "title": "軟體工程師", "company": "科技解決方案公司", "location": "城市鎮", "description": "加入我們的團隊成為軟體工程師...", "employmentType": "全職", "salary": 80000, "applicationDeadline": "2023-09-15"}
電影
一個表示電影的綱要,包含屬性如 title
、director
、release date
、genre
、duration
,和 cast members
。

schema
{ "$id": "https://example.com/movie.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "description": "A representation of a movie", "type": "object", "required": ["title", "director", "releaseDate"], "properties": { "title": { "type": "string" }, "director": { "type": "string" }, "releaseDate": { "type": "string", "format": "date" }, "genre": { "type": "string", "enum": ["Action", "Comedy", "Drama", "Science Fiction"] }, "duration": { "type": "string" }, "cast": { "type": "array", "items": { "type": "string" }, "additionalItems": false } }}
資料
{ "title": "範例電影", "director": "約翰導演", "releaseDate": "2023-07-01", "genre": "動作", "duration": "2小時 15分鐘", "cast": ["演員 A", "演員 B", "演員 C"]}
使用者個人資料
一個代表使用者個人資料的 schema,包含像是 username
、email
、fullName
、age
、location
和 interests
等屬性。

schema
{ "$id": "https://example.com/user-profile.schema.json", "$schema": "https://json-schema.dev.org.tw/draft/2020-12/schema", "description": "A representation of a user profile", "type": "object", "required": ["username", "email"], "properties": { "username": { "type": "string" }, "email": { "type": "string", "format": "email" }, "fullName": { "type": "string" }, "age": { "type": "integer", "minimum": 0 }, "location": { "type": "string" }, "interests": { "type": "array", "items": { "type": "string" } } }}
資料
{ "username": "user123", "email": "[email protected]", "fullName": "約翰·多伊", "age": 30, "location": "城市鎮", "interests": ["旅遊", "科技"]}