5 Prompt Templates to Generate Valid JSON Schemas from Natural Language
Learn how to use AI to generate JSON schemas from plain English descriptions. Includes copy-paste templates for common use cases.
The Challenge: You need to create a JSON schema for API validation, but writing schemas manually is tedious and error-prone.
What if you could describe your data structure in plain English and let AI generate the schema?
This guide provides 5 battle-tested prompt templates for generating valid JSON schemas using AI.
Why JSON Schemas Matter
The Problem They Solve
- API Validation: Ensure requests match expected structure
- Documentation: Self-documenting data contracts
- Type Safety: Catch errors before runtime
- Code Generation: Auto-generate types/classes
The Manual Approach (Painful)
Writing schemas by hand requires:
- Deep knowledge of JSON Schema syntax
- Careful attention to nested structures
- Manual validation of the schema itself
- Time and patience
The AI Approach (Fast)
- Describe your data in natural language
- AI generates the schema
- Validate and iterate quickly
The 5 Essential Templates
Template 1: Basic Object Schema
Use Case: Simple data structures with known fields
Prompt:
Generate a JSON Schema for [OBJECT_NAME].
Fields:
- [FIELD_1]: [TYPE] ([REQUIRED/OPTIONAL]) - [DESCRIPTION]
- [FIELD_2]: [TYPE] ([REQUIRED/OPTIONAL]) - [DESCRIPTION]
- [FIELD_3]: [TYPE] ([REQUIRED/OPTIONAL]) - [DESCRIPTION]
Constraints:
- [CONSTRAINT_1]
- [CONSTRAINT_2]
Include: title, description, and examples.
Example:
Generate a JSON Schema for a User object.
Fields:
- id: string (required) - Unique identifier, UUID format
- email: string (required) - Valid email address
- age: integer (optional) - Must be between 18 and 120
- roles: array of strings (required) - At least one role
Constraints:
- email must match email format
- roles cannot be empty
Include: title, description, and examples.
AI-Generated Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "User",
"description": "User object with authentication and profile information",
"type": "object",
"required": ["id", "email", "roles"],
"properties": {
"id": {
"type": "string",
"format": "uuid",
"description": "Unique identifier"
},
"email": {
"type": "string",
"format": "email",
"description": "Valid email address"
},
"age": {
"type": "integer",
"minimum": 18,
"maximum": 120,
"description": "User age"
},
"roles": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"description": "User roles"
}
},
"examples": [
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"email": "user@example.com",
"age": 30,
"roles": ["admin", "user"]
}
]
}Template 2: Nested Object Schema
Use Case: Complex data with nested structures
Prompt:
Generate a JSON Schema for [OBJECT_NAME] with nested objects.
Structure:
- [FIELD_1]: object
- [NESTED_FIELD_1]: [TYPE]
- [NESTED_FIELD_2]: [TYPE]
- [FIELD_2]: [TYPE]
All nested fields are required unless specified.
Example:
Generate a JSON Schema for an Order object with nested objects.
Structure:
- customer: object (required)
- name: string (required)
- email: string (required)
- address: object (required)
- street: string (required)
- city: string (required)
- zipCode: string (required, format: 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)
AI-Generated Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Order",
"type": "object",
"required": ["customer", "items", "total"],
"properties": {
"customer": {
"type": "object",
"required": ["name", "email", "address"],
"properties": {
"name": {"type": "string"},
"email": {"type": "string", "format": "email"},
"address": {
"type": "object",
"required": ["street", "city", "zipCode"],
"properties": {
"street": {"type": "string"},
"city": {"type": "string"},
"zipCode": {
"type": "string",
"pattern": "^[0-9]{5}$"
}
}
}
}
},
"items": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["productId", "quantity", "price"],
"properties": {
"productId": {"type": "string"},
"quantity": {"type": "integer", "minimum": 1},
"price": {"type": "number", "minimum": 0}
}
}
},
"total": {"type": "number", "minimum": 0}
}
}Validate Your Generated Schema
Paste your AI-generated JSON schema into our validator to ensure it's syntactically correct.
Validate Schema →Template 3: Enum and Const Values
Use Case: Fields with limited allowed values
Prompt:
Generate a JSON Schema for [OBJECT_NAME].
Fields with enums:
- [FIELD_1]: one of [VALUE_1, VALUE_2, VALUE_3]
- [FIELD_2]: exactly [CONST_VALUE]
- [FIELD_3]: [TYPE] with pattern [REGEX]
Example:
Generate a JSON Schema for a Product object.
Fields with enums:
- status: one of ["active", "inactive", "discontinued"]
- category: one of ["electronics", "clothing", "food"]
- currency: exactly "USD"
- sku: string matching pattern ^[A-Z]{3}-[0-9]{6}$
AI-Generated Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Product",
"type": "object",
"required": ["status", "category", "currency", "sku"],
"properties": {
"status": {
"type": "string",
"enum": ["active", "inactive", "discontinued"]
},
"category": {
"type": "string",
"enum": ["electronics", "clothing", "food"]
},
"currency": {
"type": "string",
"const": "USD"
},
"sku": {
"type": "string",
"pattern": "^[A-Z]{3}-[0-9]{6}$"
}
}
}Template 4: Conditional Schema (If/Then/Else)
Use Case: Validation rules that depend on other fields
Prompt:
Generate a JSON Schema with conditional validation.
Rule: If [FIELD_1] is [VALUE], then [FIELD_2] is required and must be [TYPE].
Otherwise, [FIELD_2] is optional.
Example:
Generate a JSON Schema for a Payment object with conditional validation.
Rule: If paymentMethod is "credit_card", then cardNumber (string, 16 digits) and cvv (string, 3 digits) are required.
If paymentMethod is "paypal", then paypalEmail (valid email) is required.
AI-Generated Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Payment",
"type": "object",
"required": ["paymentMethod"],
"properties": {
"paymentMethod": {
"type": "string",
"enum": ["credit_card", "paypal"]
},
"cardNumber": {"type": "string"},
"cvv": {"type": "string"},
"paypalEmail": {"type": "string"}
},
"if": {
"properties": {"paymentMethod": {"const": "credit_card"}}
},
"then": {
"required": ["cardNumber", "cvv"],
"properties": {
"cardNumber": {"pattern": "^[0-9]{16}$"},
"cvv": {"pattern": "^[0-9]{3}$"}
}
},
"else": {
"if": {
"properties": {"paymentMethod": {"const": "paypal"}}
},
"then": {
"required": ["paypalEmail"],
"properties": {
"paypalEmail": {"format": "email"}
}
}
}
}Template 5: Array Validation with Unique Items
Use Case: Arrays with specific constraints
Prompt:
Generate a JSON Schema for [OBJECT_NAME].
Array field: [FIELD_NAME]
- Type: array of [ITEM_TYPE]
- Min items: [NUMBER]
- Max items: [NUMBER]
- Unique items: [YES/NO]
- Item constraints: [CONSTRAINTS]
Example:
Generate a JSON Schema for a Team object.
Array field: members
- Type: array of objects
- Min items: 1
- Max items: 10
- Unique items: yes (based on userId)
- Item constraints:
- userId: string (required, UUID)
- role: one of ["admin", "member", "viewer"] (required)
AI-Generated Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Team",
"type": "object",
"required": ["members"],
"properties": {
"members": {
"type": "array",
"minItems": 1,
"maxItems": 10,
"uniqueItems": true,
"items": {
"type": "object",
"required": ["userId", "role"],
"properties": {
"userId": {
"type": "string",
"format": "uuid"
},
"role": {
"type": "string",
"enum": ["admin", "member", "viewer"]
}
}
}
}
}
}Best Practices
1. Be Specific About Types
❌ "name is a string"
✅ "name is a string, min length 1, max length 100"
2. Include Examples
Always ask for examples in the schema—they serve as documentation.
3. Validate the Schema
Use our JSON Formatter to validate the generated schema before using it.
4. Iterate on Edge Cases
If the schema doesn't handle an edge case, add it to your prompt:
Also handle:
- Empty strings should be invalid
- Null values should be rejected
5. Use Definitions for Reusability
Generate a JSON Schema with reusable definitions.
Define an Address object (street, city, zipCode).
Use this definition for both billingAddress and shippingAddress fields.
Common Mistakes to Avoid
1. Forgetting Required Fields
Always specify which fields are required.
2. No Format Validation
Use format for emails, UUIDs, dates, URIs, etc.
3. Missing Constraints
Specify min/max for numbers, minLength/maxLength for strings.
4. No Examples
Examples make schemas self-documenting.
Conclusion
AI can dramatically speed up JSON schema creation. By using these 5 templates and following best practices, you can generate production-ready schemas in minutes.
Key Takeaways:
- Use structured prompts with clear field descriptions
- Specify constraints (min/max, patterns, enums)
- Include examples for documentation
- Validate generated schemas before use
- Iterate on edge cases
Validate Your JSON Schemas
Ensure your AI-generated schemas are syntactically correct and ready for production.
Validate Now →Quick Reference
Basic Object: Fields + types + constraints
Nested Object: Structure with nested properties
Enums: Limited allowed values
Conditional: If/then/else validation
Arrays: Min/max items + uniqueness