ModernCalcs

JSON Schema Generator

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer"
      },
      "name": {
        "type": "string"
      },
      "email": {
        "type": "string",
        "format": "email"
      },
      "age": {
        "type": "integer"
      },
      "active": {
        "type": "boolean"
      },
      "score": {
        "type": [
          "number",
          "integer"
        ]
      },
      "address": {
        "type": "object",
        "properties": {
          "city": {
            "type": "string"
          },
          "zip": {
            "type": "string"
          }
        }
      },
      "tags": {
        "type": "array",
        "items": {
          "type": "string"
        }
      }
    }
  }
}

Types inferred: string, integer, number, boolean, object, array. Formats auto-detected: email, date, date-time, URI. Multiple samples merged to widen schema types. Generates Draft 2020-12 schema.

JSON Schema Generator: Bootstrap API Validation Schemas from Real Data

Writing JSON Schema by hand is tedious and error-prone. This generator takes a sample JSON payload — from a real API response, a config file, or a data fixture — and produces a Draft 2020-12 JSON Schema that describes it. Use the output as a starting point: add constraints, mark optional fields, and wire it into ajv, Pydantic, zod, or any validator.

Formula
Sample JSON: { "id": 42, "email": "a@b.com", "created": "2024-01-15", "active": true } → JSON Schema (Draft 2020-12): { "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "id": { "type": "integer" }, "email": { "type": "string", "format": "email" }, "created": { "type": "string", "format": "date" }, "active": { "type": "boolean" } }, "required": ["id", "email", "created", "active"] }

All properties found in the sample are marked required. Format hints are inferred from string content. Remove optional fields from 'required' after generating.

How Type Inference Works

The generator uses JavaScript's typeof plus Number.isInteger() to classify values: null stays null, true/false → boolean, 42 → integer, 3.14 → number, 'hello' → string, [] → array, {} → object. Nested objects recurse to produce nested schemas. Arrays inspect their items: if all items share the same type, an items schema is generated.

Format Detection for Strings

String values are tested against regex patterns for common formats: email (contains @ and a dot in the domain), date (YYYY-MM-DD), date-time (ISO 8601 with time), and uri (starts with http:// or https://). Format annotations are hints — not all validators enforce them by default. In ajv, enable 'formats' with the ajv-formats plugin.

What to Add After Generating

The generated schema describes the sample, not your business rules. After generating: mark optional fields by removing them from 'required'. Add minimum/maximum for number ranges. Add minLength/maxLength for string lengths. Add pattern for regex-validated strings (like phone numbers). Add enum for fixed value sets. Set additionalProperties: false to reject unknown keys in strict APIs.

Supported Inferences

  • null, boolean, integer, number, string types
  • Nested object → nested properties schema
  • Array items schema inferred from first item
  • format: email, date, date-time, uri detection
  • Full 'required' list from sample properties

Frequently Asked Questions

Which JSON Schema version does this generate?

This tool generates JSON Schema Draft 2020-12, identified by the $schema URI 'https://json-schema.org/draft/2020-12/schema'. This is the latest stable version. Most validators support it — ajv, Pydantic v2, TypeBox, and others.

How are types inferred from the sample?

The generator inspects each value: null → null, boolean → boolean, integer (no decimal) → integer, number with decimal → number, string → string, array → array, plain object → object. Nested objects recurse to produce nested property schemas.

What format detection is available?

String values are checked against common patterns: email addresses → format: email, ISO date strings (YYYY-MM-DD) → format: date, ISO date-time → format: date-time, HTTP/HTTPS URLs → format: uri. These are hints — validators may not enforce them by default.

Why does the schema mark all properties as required?

The schema is generated from your sample, which provides only one example of valid data. All properties found in the sample are marked required. If some are optional, remove them from the 'required' array manually.

What should I do after generating the schema?

The generated schema is a starting point: (1) Remove optional fields from 'required'. (2) Add minimum/maximum for number ranges. (3) Add minLength/maxLength for strings. (4) Add pattern for regex-validated strings. (5) Add enum for fixed value sets. (6) Add additionalProperties: false to reject unknown keys.