Trình tạo JSON Schema

Tạo JSON Schema từ dữ liệu JSON mẫu

Đầu vào JSON
Paste example JSON or import a file to infer a starting schema.
Schema được tạo
Review the generated schema, then copy or download it for further refinement.
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "age": {
      "type": "integer"
    },
    "active": {
      "type": "boolean"
    },
    "website": {
      "type": "string",
      "format": "uri"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "address": {
      "type": "object",
      "properties": {
        "street": {
          "type": "string"
        },
        "city": {
          "type": "string"
        },
        "postalCode": {
          "type": "string"
        }
      },
      "required": [
        "street",
        "city",
        "postalCode"
      ]
    },
    "projects": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "year": {
            "type": "integer"
          },
          "url": {
            "type": "string",
            "format": "uri"
          }
        },
        "required": [
          "name",
          "year"
        ]
      }
    },
    "lastSeen": {
      "type": "string",
      "format": "date-time"
    },
    "metadata": {
      "type": "null"
    }
  },
  "required": [
    "id",
    "name",
    "email",
    "age",
    "active",
    "website",
    "tags",
    "address",
    "projects",
    "lastSeen",
    "metadata"
  ]
}
Tùy chọn

JSON Schema là gì?

JSON Schema là một tiêu chuẩn để mô tả cấu trúc dữ liệu JSON. Nó cho phép biểu diễn kiểu trường, cấu trúc lồng nhau, khóa bắt buộc và các ràng buộc hữu ích cho việc kiểm tra hợp lệ.

Công cụ này làm gì

Dán JSON mẫu và công cụ sẽ suy ra schema ban đầu cho object, array, number, boolean, null và các định dạng chuỗi phổ biến. Kết quả có thể được sao chép, tải xuống và tinh chỉnh thêm.

Ví dụ

Ví dụ, với dữ liệu mẫu sau:

Đầu vào mẫu

{
  "id": "bk-101",
  "title": "In-browser Tools",
  "price": 19.99,
  "tags": ["json", "schema"],
  "published": true
}

Schema được tạo

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": { "type": "string" },
    "title": { "type": "string" },
    "price": { "type": "number" },
    "tags": {
      "type": "array",
      "items": { "type": "string" }
    },
    "published": { "type": "boolean" }
  },
  "required": ["id", "title", "price", "tags", "published"]
}

Mẹo sử dụng

  • Hãy dùng dữ liệu mẫu có tính đại diện, đặc biệt trong các mảng, để suy ra trường tùy chọn chính xác hơn.
  • Tắt “Infer required properties” nếu dữ liệu đầu vào chỉ là một ví dụ chưa đầy đủ.
  • Tắt “Allow additional properties” nếu bạn muốn schema nghiêm ngặt hơn theo mặc định.
  • Giữ bật phát hiện định dạng chuỗi để suy ra email, URI, UUID và date-time.