JSON SchemaAPIAI

JSON Schema Guide for Developers – AI, APIs, Validation

March 19, 2026·9 min read

JSON Schema is the standard way to describe the structure of JSON data. It powers API validation, form generation, documentation, and — increasingly — AI function calling. OpenAI, Anthropic, and other LLM providers use JSON Schema to define the parameters that models can output.

In this guide, we cover JSON Schema basics, common use cases, and how to generate schemas from existing JSON.

1. What Is JSON Schema?

JSON Schema is a vocabulary for annotating and validating JSON documents. A schema describes allowed types, required fields, formats (e.g., date-time, email), and constraints (min, max, pattern).

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer", "minimum": 0 },
    "email": { "type": "string", "format": "email" }
  },
  "required": ["name", "email"]
}

2. Why Generate Schemas from JSON?

Writing JSON Schema by hand is tedious. If you have a sample JSON object, you can infer a schema automatically. This is useful for:

  • AI function calling — OpenAI and Claude require JSON Schema for tool/function parameters
  • API documentation — OpenAPI uses JSON Schema for request/response bodies
  • Validation — validate incoming API payloads against a schema
  • TypeScript — generate types from JSON Schema (e.g., with json-schema-to-typescript)

3. JSON Schema Drafts

JSON Schema has evolved through several drafts. Draft 2020-12 is the latest; Draft 07 is widely supported. Our JSON Schema Generator supports Draft 2020-12, 07, and 04.

4. Implementation: Schema Inference Algorithm

Infer schema recursively from a JSON value. Type detection + format hints:

function inferSchema(value) {
  if (value === null) return { type: 'null' };
  if (typeof value === 'boolean') return { type: 'boolean' };
  if (typeof value === 'number')
    return { type: Number.isInteger(value) ? 'integer' : 'number' };
  if (typeof value === 'string') {
    const s = { type: 'string' };
    if (/^\d{4}-\d{2}-\d{2}/.test(value)) s.format = 'date-time';
    else if (/^[\w.+-]+@[\w-]+\.\w+$/.test(value)) s.format = 'email';
    else if (/^https?:\/\//.test(value)) s.format = 'uri';
    return s;
  }
  if (Array.isArray(value))
    return { type: 'array', items: value[0] ? inferSchema(value[0]) : {} };
  if (typeof value === 'object') {
    const props = {}, required = [];
    for (const [k, v] of Object.entries(value)) {
      props[k] = inferSchema(v);
      if (v != null) required.push(k);
    }
    return { type: 'object', properties: props, required };
  }
  return {};
}

5. AI Function Calling: OpenAI Format

OpenAI expects tools with parameters as JSON Schema. Example:

const tools = [{
  type: "function",
  function: {
    name: "get_weather",
    description: "Get weather for a city",
    parameters: {
      type: "object",
      properties: {
        city: { type: "string", description: "City name" },
        unit: { type: "string", enum: ["celsius", "fahrenheit"] }
      },
      required: ["city"]
    }
  }
}];

// Use with Chat Completions API
const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Weather in Tokyo?" }],
  tools
});

Generate the parameters object from a sample JSON with our JSON Schema Generator, then add description and enum as needed.

6. How to Use the Online Generator

  1. Paste a JSON sample — any valid JSON object or array.
  2. Choose a draft — 2020-12, 07, or 04.
  3. Click “Generate” — copy the schema into your API or AI tool definition.

💡 All processing happens in your browser. Your JSON never leaves your device.

7. Conclusion

JSON Schema is indispensable for modern API and AI development. Generating schemas from JSON samples saves time and reduces errors.

Try our free JSON Schema Generator for instant schema inference — no upload, no account required.