---
name: json-schema-generator
description: "A strict, valid JSON Schema inferred from one sample payload: correct types and nesting, required and additionalProperties set the way you want, formats and enums inferred only where the sample justifies them, and every guess flagged for review. Use when a sample JSON payload or API response needs a strict JSON Schema for request validation, with required fields, formats, and enums inferred and the guesses flagged. Do not use for TypeScript type generation, OpenAPI documents, or GraphQL schemas. Related topics: coding, api, utility."
---

# JSON Schema Generator

A strict, valid JSON Schema inferred from one sample payload: correct types and nesting, required and additionalProperties set the way you want, formats and enums inferred only where the sample justifies them, and every guess flagged for review.

## When to use

Use this skill when a sample JSON payload or API response needs a strict JSON Schema for request validation, with required fields, formats, and enums inferred and the guesses flagged. Do not use it for TypeScript type generation, OpenAPI documents, or GraphQL schemas.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Walk the sample recursively and assign each value its precise type, distinguishing integer from number, null from absent, and arrays from tuples.
3. Merge multiple samples so inconsistent fields become optional, mixed types become type lists, and repeated values become enum candidates.
4. Apply the required and additionalProperties toggles at every object level and factor repeated objects into definitions for the chosen draft.
5. Infer only the selected constraint kinds and only where the sample justifies them, keeping every keyword valid for the draft.
6. Output the schema and list each inferred constraint or enum that rests on limited evidence.

## Inputs

- **Sample JSON** (longer free text)
- **Schema draft** (choose one: Draft-07, Draft 2019-09, Draft 2020-12)
- **What the payload is** (free text, e.g. "an order webhook body or a user profile response")
- **Mark all fields required** (choose one: Yes, No)
- **Allow additional or unknown properties** (choose one: Yes, No)
- **Constraints to infer** (choose any: Min/Max length for strings, Minimum/Maximum for numbers, Enums based on values, Date/Time formats, Email and URI formats, Array item limits)
- **Add titles and descriptions** (choose one: Yes, No)

## Output

One fenced json block with the complete schema for the chosen draft, followed by a bullet list of inferred constraints and enums to review.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### JSON Schema Generator

Turn my sample JSON into a JSON Schema I can validate requests against.

#### Payload
- Sample JSON: {{Sample JSON}}
- Schema draft: {{Schema draft}}
- What the payload is: {{What the payload is}}

#### Rules
- Mark all fields required: {{Mark all fields required}}
- Allow additional or unknown properties: {{Allow additional or unknown properties}}
- Constraints to infer: {{Constraints to infer}}
- Add titles and descriptions: {{Add titles and descriptions}}

Walk the sample recursively and emit a type for every value, distinguishing integer from number, null from absent, and arrays with uniform items from tuples. Where several samples are given, merge them: a field present in some and not others is optional, a field with mixed types gets a type list, and values that repeat across samples become enum candidates. Set required and additionalProperties from the toggles at every object level, and add $schema, $id, and definitions (or $defs for 2019-09 and later) for any object that appears more than once. Infer constraints only from the selected kinds and only when the sample supports them: a string that parses as RFC 3339 gets format date-time, while a value that looks like an ID gets no length bound from one example. Keep the schema valid for the chosen draft, with no 2020-12 keywords in a Draft-07 document. Output the schema in one fenced json block, then a short list of every inferred constraint or enum that is a guess from limited data.

## Pitfalls

- Turning the three values seen in one sample into a closed enum, which rejects the fourth legitimate value in production.
- Setting minLength and maxLength from a single string, which makes the schema fail on any other valid input.
- Using $defs and unevaluatedProperties in a Draft-07 schema, which the validator ignores or rejects.
