Oh MyUtils

JSON Schema 生成器 - 从JSON数据生成模式 在线工具

从JSON数据生成JSON Schema,用于API验证、文档和代码生成。支持Draft-04、Draft-07和2020-12 — 100%客户端处理,无数据发送到服务器。

Schema草案
必填字段
格式检测
额外属性
缩进
JSON输入
JSON Schema输出
 

常见问题

什么是JSON Schema生成器?

JSON Schema生成器是一种自动从示例JSON数据创建JSON Schema定义的工具。JSON Schema是描述JSON文档结构、类型和约束的标准词汇表(在json-schema.org上定义)。无需手动编写模式定义,只需粘贴JSON示例,工具就会推断出正确的模式,包括对象属性、数据类型、必填字段、数组项类型以及日期和电子邮件等字符串格式。

如何从JSON数据生成JSON Schema?

将JSON数据粘贴到左侧的输入框中。工具会立即在右侧的输出面板中生成JSON Schema。您可以通过选择模式草案版本(Draft-04、Draft-07或2020-12)、切换必填字段检测、启用日期和电子邮件等模式的字符串格式检测、控制是否允许额外属性来自定义输出。

使用此工具时我的JSON数据安全吗?

是的,完全安全。此工具使用客户端JavaScript在浏览器中处理所有内容。您的JSON数据永远不会离开您的计算机——没有服务器上传,没有API调用,也没有数据存储。

我应该使用哪个JSON Schema草案版本?

根据您的生态系统选择:Draft 2020-12(默认)是最新的稳定标准,在OpenAPI 3.1中使用。Draft-07被验证库和工具广泛支持,是最大兼容性的安全选择。Draft-04用于遗留系统和OpenAPI 3.0兼容性。

工具如何确定必填字段?

默认情况下,工具将生成的模式中所有非null属性标记为必填。具有null值的字段被排除在required数组之外,因为null表示该字段可能不存在或为可选。您可以完全关闭此行为。

工具检测哪些字符串格式?

启用格式检测后,工具会分析字符串值并自动为识别的模式添加'format'关键字:date-time(ISO 8601时间戳)、date、time、电子邮件地址、URI/URL、UUID、IPv4地址和IPv6地址。

'额外属性'选项有什么作用?

JSON Schema中的'additionalProperties'关键字控制对象是否可以包含模式中未列出的属性。设置为'允许'(默认)时,生成的模式允许额外字段。设置为'拒绝'时,向对象模式添加'"additionalProperties": false',创建拒绝意外字段的严格契约。

代码示例

// JSON to JSON Schema generator
function jsonToSchema(json, options = {}) {
  const { draft = '2020-12', includeRequired = true, detectFormats = true } = options;
  const schemaUri = {
    'draft-04': 'http://json-schema.org/draft-04/schema#',
    'draft-07': 'http://json-schema.org/draft-07/schema#',
    '2020-12': 'https://json-schema.org/draft/2020-12/schema',
  }[draft];

  function inferSchema(value) {
    if (value === null) return { type: 'null' };
    if (typeof value === 'boolean') return { type: 'boolean' };
    if (typeof value === 'number') {
      return { type: Number.isInteger(value) ? 'integer' : 'number' };
    }
    if (typeof value === 'string') {
      const schema = { type: 'string' };
      if (detectFormats) {
        if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(value)) schema.format = 'date-time';
        else if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) schema.format = 'email';
        else if (/^https?:\/\//.test(value)) schema.format = 'uri';
      }
      return schema;
    }
    if (Array.isArray(value)) {
      if (value.length === 0) return { type: 'array', items: {} };
      const itemSchemas = value.map(inferSchema);
      const unique = dedup(itemSchemas);
      return { type: 'array', items: unique.length === 1 ? unique[0] : { oneOf: unique } };
    }
    if (typeof value === 'object') {
      const properties = {};
      const required = [];
      for (const [key, val] of Object.entries(value)) {
        properties[key] = inferSchema(val);
        if (includeRequired && val !== null) required.push(key);
      }
      const schema = { type: 'object', properties };
      if (required.length > 0) schema.required = required;
      return schema;
    }
    return {};
  }

  function dedup(schemas) {
    const seen = new Map();
    for (const s of schemas) {
      const key = JSON.stringify(s);
      if (!seen.has(key)) seen.set(key, s);
    }
    return [...seen.values()];
  }

  const parsed = JSON.parse(json);
  return { $schema: schemaUri, ...inferSchema(parsed) };
}

// Usage
const json = '{"name":"Alice","age":30,"email":"alice@example.com"}';
console.log(JSON.stringify(jsonToSchema(json), null, 2));

相关工具