Back to Blog
9 min read

Prompting for Schema: How to Generate JSON Schemas from Text Descriptions

Learn how to use AI to automatically generate JSON schemas from plain English descriptions. Includes templates for validation, API design, and data modeling.

TL;DR: AI can generate complete JSON schemas from natural language descriptions. Provide field names, types, constraints, and examples—get production-ready schemas in seconds.

The Problem: Manual Schema Writing is Tedious

You need a JSON schema for API validation. Writing it manually means:

  • Learning JSON Schema syntax
  • Defining every field, type, and constraint
  • Handling nested structures
  • Testing edge cases

Hours of work. AI can do it in 30 seconds.

What is JSON Schema?

JSON Schema is a vocabulary for validating JSON structure:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["name", "email"],
  "properties": {
    "name": {"type": "string", "minLength": 1},
    "email": {"type": "string", "format": "email"}
  }
}

Use cases:

  • API request/response validation
  • Configuration file validation
  • Data contract documentation
  • Code generation (TypeScript types, etc.)

The AI Schema Generation Workflow

Step 1: Describe Your Data Structure

Template:

I need a JSON schema for {OBJECT_NAME}.

Fields:
- {FIELD_1}: {TYPE} ({REQUIRED/OPTIONAL}) - {CONSTRAINTS}
- {FIELD_2}: {TYPE} ({REQUIRED/OPTIONAL}) - {CONSTRAINTS}

Generate a JSON Schema (draft-07).

Step 2: Add Validation Rules

Specify:

  • Required vs optional fields
  • Data types (string, number, boolean, array, object)
  • Constraints (min/max, patterns, enums)
  • Nested structures

Step 3: Validate the Schema

Use our JSON Validator to ensure the schema itself is valid JSON.

Copy-Paste Schema Generation Templates

Template 1: Basic Object Schema

Generate a JSON Schema (draft-07) for a User object.

Fields:
- id: string (required) - UUID format
- username: string (required) - 3-20 characters, alphanumeric only
- email: string (required) - valid email format
- age: number (optional) - integer between 13 and 120
- active: boolean (required) - default true

Include title, description, and examples.

Output ONLY the JSON schema:

AI Output:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "User",
  "description": "User account object",
  "type": "object",
  "required": ["id", "username", "email", "active"],
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid",
      "description": "Unique user identifier"
    },
    "username": {
      "type": "string",
      "minLength": 3,
      "maxLength": 20,
      "pattern": "^[a-zA-Z0-9]+$",
      "description": "Alphanumeric username"
    },
    "email": {
      "type": "string",
      "format": "email",
      "description": "User email address"
    },
    "age": {
      "type": "integer",
      "minimum": 13,
      "maximum": 120,
      "description": "User age"
    },
    "active": {
      "type": "boolean",
      "default": true,
      "description": "Account active status"
    }
  },
  "examples": [
    {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "username": "alice123",
      "email": "alice@example.com",
      "age": 30,
      "active": true
    }
  ]
}

Template 2: Nested Object Schema

Generate a JSON Schema for an Order object with nested structures.

Structure:
- orderId: string (required) - format ORD-XXXXX
- customer: object (required)
  - name: string (required)
  - email: string (required) - email format
  - address: object (required)
    - street: string (required)
    - city: string (required)
    - zipCode: string (required) - 5 digits
- items: array of objects (required, min 1 item)
  - productId: string (required)
  - quantity: integer (required, min 1)
  - price: number (required, min 0)
- total: number (required, min 0)

Output JSON Schema:

Template 3: Enum and Pattern Constraints

Generate a JSON Schema for a Product object with enums and patterns.

Fields:
- sku: string (required) - pattern ^[A-Z]{3}-[0-9]{6}$
- status: string (required) - enum ["active", "inactive", "discontinued"]
- category: string (required) - enum ["electronics", "clothing", "food"]
- price: number (required) - minimum 0, multipleOf 0.01
- currency: string (required) - const "USD"

Output JSON Schema:

AI Output:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["sku", "status", "category", "price", "currency"],
  "properties": {
    "sku": {
      "type": "string",
      "pattern": "^[A-Z]{3}-[0-9]{6}$"
    },
    "status": {
      "type": "string",
      "enum": ["active", "inactive", "discontinued"]
    },
    "category": {
      "type": "string",
      "enum": ["electronics", "clothing", "food"]
    },
    "price": {
      "type": "number",
      "minimum": 0,
      "multipleOf": 0.01
    },
    "currency": {
      "type": "string",
      "const": "USD"
    }
  }
}

Template 4: Conditional Schema (if/then/else)

Generate a JSON Schema with conditional validation.

Base object:
- paymentMethod: string (required) - enum ["credit_card", "paypal", "bank_transfer"]

Conditional rules:
- If paymentMethod is "credit_card", then require:
  - cardNumber: string (16 digits)
  - cvv: string (3 digits)
  - expiryDate: string (MM/YY format)
  
- If paymentMethod is "paypal", then require:
  - paypalEmail: string (email format)
  
- If paymentMethod is "bank_transfer", then require:
  - accountNumber: string
  - routingNumber: string (9 digits)

Output JSON Schema with if/then/else:

Template 5: Array with Unique Items

Generate a JSON Schema for a Team object.

Fields:
- teamId: string (required) - UUID
- name: string (required) - 1-100 characters
- members: array of objects (required, min 1, max 50, unique by userId)
  - userId: string (required) - UUID
  - role: string (required) - enum ["owner", "admin", "member"]
  - joinedAt: string (required) - ISO 8601 date

Ensure members array has unique userId values.

Output JSON Schema:

Validate Your Generated Schema

Paste your AI-generated JSON schema into our validator to ensure it's syntactically correct.

Validate Schema →

Advanced Prompting Techniques

Technique 1: Provide Example Data

Generate a JSON Schema for this example data:

{
  "productId": "PROD-123456",
  "name": "Laptop",
  "price": 999.99,
  "inStock": true,
  "tags": ["electronics", "computers"]
}

Infer types and add appropriate constraints.

Technique 2: Specify Draft Version

Generate a JSON Schema using draft-2020-12 (latest version).

[YOUR DESCRIPTION]

Use modern keywords like prefixItems, $defs, etc.

Technique 3: Request Definitions for Reusability

Generate a JSON Schema with reusable definitions.

Define an "Address" object (street, city, state, zipCode).
Use this definition for both billingAddress and shippingAddress fields.

Output schema with $defs:

Real-World Use Cases

Use Case 1: API Request Validation

Prompt:

Generate a JSON Schema for a user registration API request.

Required fields:
- email: valid email
- password: min 8 chars, must contain uppercase, lowercase, number, special char
- username: 3-20 chars, alphanumeric
- acceptTerms: boolean, must be true

Optional fields:
- referralCode: string, pattern ^REF-[A-Z0-9]{6}$

Output schema:

Use Case 2: Configuration File Validation

Prompt:

Generate a JSON Schema for an application config file.

Structure:
- server: object
  - port: integer (1024-65535)
  - host: string (enum: ["localhost", "0.0.0.0"])
  - ssl: boolean
- database: object
  - url: string (URI format)
  - poolSize: integer (1-100)
- logging: object
  - level: string (enum: ["debug", "info", "warn", "error"])
  - file: string

Output schema:

Use Case 3: Form Validation

Prompt:

Generate a JSON Schema for a contact form submission.

Fields:
- name: string (required, 1-100 chars)
- email: string (required, email format)
- phone: string (optional, pattern: ^\+?[0-9]{10,15}$)
- message: string (required, 10-1000 chars)
- subscribe: boolean (optional, default false)

Output schema:

Pro Tips

Tip 1: Be Specific About Constraints

Vague: "age should be reasonable"
Specific: "age: integer, minimum 0, maximum 120"

Tip 2: Include Examples

Always request examples in the schema:

Include 2-3 examples of valid data matching this schema.

Tip 3: Validate Against Real Data

After generation:

  1. Get sample data
  2. Validate against schema
  3. Refine schema if needed

Tip 4: Use Descriptions

Add description fields to all properties explaining their purpose.

Common Mistakes to Avoid

Mistake 1: Forgetting Required Fields

Always specify which fields are required:

Required: id, name, email
Optional: age, phone

Mistake 2: No Format Validation

Use format keywords:

  • "format": "email" for emails
  • "format": "uuid" for UUIDs
  • "format": "date-time" for ISO dates
  • "format": "uri" for URLs

Mistake 3: Missing Constraints

Add constraints for robustness:

  • minLength/maxLength for strings
  • minimum/maximum for numbers
  • minItems/maxItems for arrays
  • pattern for specific formats

Automation Script

import openai
import json
 
def generate_schema(description):
    prompt = f"""
    Generate a JSON Schema (draft-07) for the following description.
    
    {description}
    
    Output ONLY the JSON schema, no explanations:
    """
    
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    
    schema = response.choices[0].message.content
    
    # Validate it's valid JSON
    try:
        json.loads(schema)
        return schema
    except:
        return None
 
# Usage
desc = """
User object:
- id: UUID (required)
- name: string (required, 1-100 chars)
- email: email format (required)
"""
 
schema = generate_schema(desc)
print(schema)

Conclusion

AI-powered JSON schema generation saves hours of manual work. By providing clear descriptions, constraints, and examples, you can generate production-ready schemas instantly.

Key Takeaways:

  • Describe structure clearly
  • Specify all constraints
  • Include required/optional fields
  • Request examples
  • Validate generated schemas

Master Template:

Generate JSON Schema (draft-07) for {OBJECT_NAME}.

Fields:
- {FIELD}: {TYPE} ({REQUIRED/OPTIONAL}) - {CONSTRAINTS}

Include title, description, examples.

Output ONLY JSON schema:

Validate Your Schemas

Ensure your AI-generated JSON schemas are syntactically correct and ready for production use.

Validate Now →