JSON Schema Generator

Automatically generate standard-compliant JSON Schema definitions from JSON data

JSON Input
Lines: 1 | Characters: 0
JSON Schema Output
// JSON Schema will be generated automatically
Properties: 0 | Lines: 1

Configuration Options

Schema ID
Set a unique identifier for the Schema, typically in URI format, used for referencing and identifying Schema in large systems
All Fields Required
When enabled, all properties are added to the required array, meaning these fields must exist in the data
Add Default Values
Auto-generate default property based on actual values in sample JSON, useful for config files needing defaults
Add Field Descriptions
Auto-add description field for each property, describing data type and source, helpful for documentation
Strict Mode
Enable additionalProperties: false to prohibit undefined extra properties, ensuring strict data structure consistency

What is JSON Schema?

JSON Schema is a specification for describing and validating JSON data structures. It defines rules for structure, data types, constraints, and is widely used for API validation, form validation, config file verification, and more.

With JSON Schema, you can:

  • Data Validation: Verify JSON data matches expected format and constraints
  • Documentation Generation: Auto-generate API docs and type definitions
  • Code Intelligence: Get smart completion and type checking in IDEs
  • Automated Testing: Auto-validate response data format in tests

This tool supports JSON Schema Draft-07 specification, intelligently inferring nested objects, array types, enum values, and other complex structures to help you quickly create standardized Schema definitions.

How to Use

  1. Paste JSON data in the left input box, or click Sample to load example data
  2. Configure options: set Schema ID, choose whether all fields are required, add default values, etc.
  3. The corresponding JSON Schema definition is generated automatically on the right
  4. Click Copy to copy the generated Schema to clipboard
  5. Use the Schema for API validation, form validation, or config file verification

Examples

Simple Object Schema

输入 JSON:
{
  "name": "张三",
  "age": 28,
  "isActive": true
}

输出 JSON Schema:
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "description": "Object with 3 properties",
  "properties": {
    "name": {
      "type": "string",
      "description": "String value"
    },
    "age": {
      "type": "integer",
      "description": "Number value"
    },
    "isActive": {
      "type": "boolean",
      "description": "Boolean value"
    }
  },
  "required": ["name", "age", "isActive"]
}

Nested Objects and Arrays

输入 JSON:
{
  "user": {
    "id": 1,
    "profile": {
      "avatar": "url",
      "skills": ["JS", "TS"]
    }
  }
}

输出 JSON Schema:
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "user": {
      "type": "object",
      "properties": {
        "id": { "type": "integer" },
        "profile": {
          "type": "object",
          "properties": {
            "avatar": { "type": "string" },
            "skills": {
              "type": "array",
              "items": { "type": "string" }
            }
          }
        }
      }
    }
  }
}

Data Validation Example

使用 Ajv 验证数据:

const Ajv = require('ajv');
const ajv = new Ajv();

const schema = { /* 生成的 Schema */ };
const validate = ajv.compile(schema);

const validData = { name: "李四", age: 25, isActive: false };
const invalidData = { name: "王五", age: "三十" };

console.log(validate(validData));    // true
console.log(validate(invalidData));  // false
console.log(validate.errors);        // 错误详情

FAQ

Q: What are common use cases for JSON Schema?

A: JSON Schema is mainly used for: API request/response validation, form data validation, config file format checking, database document constraints, automated test data validation, IDE smart completion configuration. It's an important tool for data specification in modern web development.

Q: What Schema version does this tool generate?

A: This tool generates Schema following JSON Schema Draft-07 specification, the most widely supported version, compatible with most validation libraries and toolchains.

Q: How are complex array types handled?

A: The tool automatically analyzes array element structure. If elements are objects, items definition is generated; if types are inconsistent, anyOf or oneOf represents multiple possible types; empty arrays generate empty items definition.

Q: What does the required field mean?

A: required is a string array listing property names that must exist. If a property is in required, it cannot be missing during validation. Properties not in required are optional.

Q: How to use the generated Schema in projects?

A: Use validation libraries like Ajv, jsonschema for data validation. In Node.js: const Ajv = require('ajv'); const validate = new Ajv().compile(schema); const valid = validate(data);. Front-end frameworks like React, Vue have form validation components supporting JSON Schema.

Q: What does additionalProperties: false do?

A: This setting prohibits extra properties not defined in Schema. Useful for scenarios needing strict data structure control, preventing accidental field pollution, but may cause compatibility issues if too strict.

Q: Can I add custom validation rules?

A: Yes, manually edit Schema after generation to add more constraints. JSON Schema supports rich validation keywords: minimum/maximum (number range), minLength/maxLength (string length), pattern (regex match), enum (enum values), format (predefined formats like email, uri), etc.

Q: How to generate JSON Schema from TypeScript types?

A: This tool infers Schema from JSON data. To generate from TypeScript types, use tools like typescript-json-schema, or first create sample JSON data then use this tool.