Skip to main content
Cloud & AI Hub
Browse
Glossary AI Directory Playgrounds Models Prompts Explainers Strategy Matrix Benchmark Decoder

JSON Schema Generator

Use Case: Generate a valid JSON Schema (Draft 7) from one or more example JSON objects

System Instructions

You are a JSON Schema expert. Infer a strict, well-annotated JSON Schema Draft 7 from example JSON objects. Output only valid JSON.

User Prompt Template

Generate a JSON Schema for the following example object(s):

{EXAMPLE_JSON}

Additional constraints: {CONSTRAINTS}

Run This Prompt โ€” SDK Snippets

Implementation Guidelines

What This Prompt Does

This prompt reverse-engineers a JSON Schema Draft 7 document from one or more concrete JSON examples. It infers field types, optionality (by comparing multiple examples), string formats (email, uri, date-time), and reasonable validation constraints like minLength, minimum, and enum values. This is invaluable for bootstrapping schema validation in APIs, form validation libraries, or data pipeline contracts.

System Prompt

You are a JSON Schema expert (Draft 7 / Draft 2020-12 compatible).

Given one or more example JSON objects, generate a complete, strict JSON Schema by:

1. Inferring types for every field (string, number, integer, boolean, array, object, null).
2. Marking fields as required if they appear in ALL provided examples; optional otherwise.
3. Detecting string formats: "email", "uri", "date-time", "uuid", "ipv4" โ€” apply the 
   "format" keyword where confident.
4. Adding "description" annotations for every property (infer from the key name and value).
5. Setting reasonable "minLength"/"maxLength" for strings, "minimum"/"maximum" for numbers 
   based on observed values. Be conservative โ€” do not over-constrain.
6. For arrays, generate an "items" schema from the observed elements.
7. For enumerations (field always contains one of a small set of values), use "enum".

Output ONLY a valid JSON object representing the JSON Schema. 
Do not output markdown, explanations, or prose โ€” only the raw JSON schema.

User Prompt Template

Generate a JSON Schema (Draft 7) for the following JSON example(s).

Example JSON:
{EXAMPLE_JSON}

Additional constraints or notes:
{CONSTRAINTS}

Output the JSON Schema now:

Example Output

Input {EXAMPLE_JSON}:

[
  {"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890", "email": "[email protected]", "age": 28, "role": "admin", "active": true, "created_at": "2024-01-15T10:30:00Z"},
  {"id": "b2c3d4e5-f6a7-8901-bcde-f12345678901", "email": "[email protected]", "age": 34, "role": "viewer", "active": false, "created_at": "2024-03-22T08:00:00Z", "bio": "Software engineer"}
]

Model output:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "User",
  "type": "object",
  "required": ["id", "email", "age", "role", "active", "created_at"],
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid",
      "description": "Unique identifier for the user (UUID v4)"
    },
    "email": {
      "type": "string",
      "format": "email",
      "description": "User's email address",
      "maxLength": 254
    },
    "age": {
      "type": "integer",
      "description": "User's age in years",
      "minimum": 0,
      "maximum": 150
    },
    "role": {
      "type": "string",
      "description": "User's access role",
      "enum": ["admin", "viewer"]
    },
    "active": {
      "type": "boolean",
      "description": "Whether the user account is currently active"
    },
    "created_at": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601 timestamp of when the account was created"
    },
    "bio": {
      "type": "string",
      "description": "Optional freeform biography text",
      "maxLength": 1000
    }
  },
  "additionalProperties": false
}

Tips & Variations

  • Provide multiple diverse examples: The more varied the examples (different field combinations, edge case values), the more accurate the optionality and type inference. A single example will mark every field as required.
  • Add additionalProperties: false explicitly: Include "Reject any undeclared fields with additionalProperties: false" in {CONSTRAINTS} to produce a strict schema. Omit for lenient validation schemas.
  • OpenAPI 3.0 variant: Change the system prompt to target OpenAPI 3.0 Schema Objects (which differ from JSON Schema Draft 7 in key ways โ€” no $schema, uses nullable: true instead of type: ["string", "null"]). Add a note about the target format in {CONSTRAINTS}.
  • Round-trip validation: Pipe the generated schema into ajv (JS) or jsonschema (Python) and validate the original examples against it to catch any over-constrained inference errors.