Chuyển đổi JSON Schema API
Chuyển JSON Schema sang TypeScript, Zod hoặc Pydantic, nâng draft-04/06/07 lên 2020-12, chuyển qua lại OpenAPI 3.0, gộp hoặc giải tham chiếu $ref.
Gửi JSON Schema (đối tượng, chuỗi JSON hoặc YAML, file hoặc URL) và chọn to. Sinh code: typescript (interface / type kèm JSDoc), zod (schema có ràng buộc min / max / regex / email / uuid / datetime, .strict(), z.lazy cho kiểu đệ quy, kèm kiểu z.infer) và pydantic (model Pydantic v2 với ràng buộc Field(), alias cho khoá không phải định danh hợp lệ, extra="forbid"); definitions / $defs thành các kiểu có tên.
Phiên bản: 2020-12, 2019-09, draft-07, draft-06, draft-04 để nâng hoặc hạ phiên bản (definitions ↔ $defs, id ↔ $id, exclusiveMinimum / Maximum dạng boolean ↔ số, items dạng mảng ↔ prefixItems, dependencies ↔ dependentRequired / dependentSchemas, const ↔ enum, từ khoá đứng cạnh $ref). OpenAPI: openapi-3.0 đổi JSON Schema thành Schema Object của OpenAPI 3.0 (nullable, example, giới hạn exclusive, $defs → components.schemas); json-schema làm chiều ngược lại và nhận cả tài liệu OpenAPI 3.0 (hoặc Swagger 2.0). Tham chiếu: bundle gom các $ref bên ngoài (từ refs = {"common.json": {...}} hoặc URL với fetch=1) vào $defs; dereference thay mọi $ref bằng nội dung và báo vòng lặp (giữ lại dạng $ref, hoặc circular=error).
Những gì không biểu diễn được ở định dạng đích được liệt kê trong warnings. Liên quan: /v1/json-to-types suy kiểu từ dữ liệu mẫu, /v1/schema-mock tạo dữ liệu ví dụ, /v1/openapi-tools xử lý cả tài liệu API. format=text chỉ trả về code.
Markdown · OpenAPI · Postman · llms.txt
Tham số
| Tên | Vị trí | Kiểu | Bắt buộc | Mặc định | |
|---|---|---|---|---|---|
schema | body | object | có | JSON Schema (đối tượng, hoặc chuỗi JSON / YAML; tối đa 3 MB). Hoặc file / url. | |
to | body|query | string | không | typescript | typescript, zod, pydantic, 2020-12, 2019-09, draft-07, draft-06, draft-04, openapi-3.0, json-schema, bundle hoặc dereference. |
from | body|query | string | không | Phiên bản nguồn khi thiếu $schema: draft-04 … 2020-12 hoặc openapi-3.0 (tự nhận diện). | |
name | body|query | string | không | Tên kiểu gốc (mặc định: title hoặc Root). | |
refs | body | object | không | Tài liệu ngoài cho $ref, khoá là URI hoặc tên file. | |
fetch | body|query | boolean | không | 1: tải các $ref trỏ tới URL http(s) (tối đa 20). | |
circular | body|query | string | không | ref | dereference: ref (giữ vòng lặp dạng $ref) hoặc error. |
format | query | string | không | text: chỉ trả về code dạng text/plain. |
Ví dụ
curl -X POST "https://apibox.io.vn/v1/schema-convert" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"schema":{"title":"Order","type":"object","required":["id","items"],"properties":{"id":{"type":"string","format":"uuid"},"items":{"type":"array","minItems":1,"items":{"$ref":"#/$defs/Item"}},"note":{"type":["string","null"],"maxLength":500}},"$defs":{"Item":{"type":"object","required":["sku","qty"],"properties":{"sku":{"type":"string"},"qty":{"type":"integer","minimum":1}}}}},"to":"zod"}'const res = await fetch('https://apibox.io.vn/v1/schema-convert', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"schema": {
"title": "Order",
"type": "object",
"required": [
"id",
"items"
],
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"items": {
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/$defs/Item"
}
},
"note": {
"type": [
"string",
"null"
],
"maxLength": 500
}
},
"$defs": {
"Item": {
"type": "object",
"required": [
"sku",
"qty"
],
"properties": {
"sku": {
"type": "string"
},
"qty": {
"type": "integer",
"minimum": 1
}
}
}
}
},
"to": "zod"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/schema-convert",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"schema": {"title": "Order", "type": "object", "required": ["id", "items"], "properties": {"id": {"type": "string", "format": "uuid"}, "items": {"type": "array", "minItems": 1, "items": {"$ref": "#/$defs/Item"}}, "note": {"type": ["string", "null"], "maxLength": 500}}, "$defs": {"Item": {"type": "object", "required": ["sku", "qty"], "properties": {"sku": {"type": "string"}, "qty": {"type": "integer", "minimum": 1}}}}}, "to": "zod"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/schema-convert');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"schema":{"title":"Order","type":"object","required":["id","items"],"properties":{"id":{"type":"string","format":"uuid"},"items":{"type":"array","minItems":1,"items":{"$ref":"#/$defs/Item"}},"note":{"type":["string","null"],"maxLength":500}},"$defs":{"Item":{"type":"object","required":["sku","qty"],"properties":{"sku":{"type":"string"},"qty":{"type":"integer","minimum":1}}}}},"to":"zod"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Kết quả trả về
{
"to": "zod",
"language": "zod",
"code": "// Generated from JSON Schema by apibox.io.vn /v1/schema-convert\nimport { z } from 'zod';\n\nexport const ItemSchema = z.object({\n sku: z.string(),\n qty: z.number().int().gte(1),\n});\nexport type Item = z.infer<typeof ItemSchema>;\n\nexport const OrderSchema = z.object({\n id: z.string().uuid(),\n items: z.array(ItemSchema).min(1),\n note: z.string().max(500).nullable().optional(),\n});\nexport type Order = z.infer<typeof OrderSchema>;\n",
"types": [
"Order",
"Item"
],
"warnings": []
}
Ứng dụng
- Sinh kiểu TypeScript và bộ kiểm tra Zod từ JSON Schema của API
- Chuyển schema draft-04 / draft-07 cũ lên 2020-12 cho các validator mới
- Dùng lại JSON Schema trong đặc tả OpenAPI 3.0 (và ngược lại)
- Tạo model Pydantic hoặc schema tự chứa cho tool calling của LLM
Hỏi đáp
Sinh code cho Zod phiên bản nào?
API của Zod 3 (z.object, z.string().email()…), Zod 4 vẫn chấp nhận.
Có chuyển TypeScript sang JSON Schema được không?
Không, việc đó cần trình biên dịch TypeScript. Hãy dùng JSON Schema làm nguồn, hoặc suy kiểu từ dữ liệu mẫu bằng json-to-types.
$ref trỏ ra ngoài thì sao?
Gửi các tài liệu được tham chiếu trong refs, hoặc đặt fetch=1 để tải URL http(s) công khai (tối đa 20 tài liệu).
API liên quan
Kiểm tra JSON
Kiểm tra cú pháp JSON, báo đúng dòng và cột bị lỗi, và validate dữ liệu theo JSON Schema (draft 4, 7, 2019-09 hoặc 2020-12).
{ }Định dạng JSON
Làm đẹp JSON với thụt lề 0-8 dấu cách hoặc tab, rút gọn (minify) hoặc sắp xếp key theo bảng chữ cái. Báo lỗi cú pháp kèm dòng và cột.
CSVCSV sang JSON
Chuyển CSV thành mảng object JSON theo dòng tiêu đề, tự nhận dấu phân cách, tự chuyển số và boolean, báo lỗi từng dòng.