By Vivek - October 6, 2026

Generate JSON Schema from a Sample API Response

A sample API response shows what happened once. JSON Schema describes the values a response is allowed to contain. Generating a schema from a sample gives you a starting point, but the API contract must decide which fields are required, which types are allowed and which constraints apply.

Open the JSON Schema Generator. It infers Draft 2020-12 schemas from sample data; it does not validate responses against the generated schema.

Generate a schema from two records

Paste this array into Sample JSON input:

[
  { "id": 101, "name": "Ada", "active": true },
  { "id": 102, "name": "Grace" }
]

Set Schema title to UsersResponse, leave Infer required fields on and Allow extra fields off, then click Generate. The expected schema is:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "UsersResponse",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": { "type": "integer" },
      "name": { "type": "string" },
      "active": { "type": "boolean" }
    },
    "additionalProperties": false,
    "required": ["id", "name"]
  }
}

The sample contains an array of objects. Both records have id and name, so those properties appear in required. Only one has active, so that field is optional in the inferred item schema. Its type is still boolean when present.

Review required fields and extra properties

The generator’s default excludes extra fields from each inferred object. If this schema were used by a compatible validator, a record containing an unlisted email property would fail. Turn Allow extra fields on and regenerate if your contract permits additional properties.

Turning Infer required fields off omits the inferred required lists. That allows properties to be absent; it does not remove their type constraints when present. The official JSON Schema object reference explains how properties, required and additionalProperties interact.

An optional field is different from a nullable field. If active can be explicitly null, include a representative null value or edit the schema to allow it. When the sample contains both boolean and null values, this tool expresses the alternatives with anyOf.

Avoid overfitting one successful response

A single object makes every observed property look required when inference is enabled. An empty sample array cannot establish its item shape: the tool produces an unconstrained items schema. Include representative records with absent fields, nullable values and nested collections before reviewing the result.

Numeric inference also needs judgement. Whole numbers infer integer; include a fractional value if the field can contain one. A sample value of 1 does not prove the API rejects 1.5. Similarly, a string that looks like a date stays a string. The generator does not infer date formats, enums, minimum lengths or numerical limits.

Add those rules from the contract, rather than treating every observed value as a complete list of possibilities. Keep $schema aligned with the draft your validator supports.

Use the schema in a contract check

Copy or download the generated schema, review it, then use a Draft 2020-12 compatible validator in your application’s tests or request workflow. Check both valid examples and deliberately invalid cases, such as a missing id, a string instead of an integer, or an unexpected property when extras are forbidden.

If the response has an envelope, extract the records with JSONPath before generating a records-only schema. Keep the envelope when that is what you intend to validate. For differences between two responses, use JSON Diff with record IDs. Return to the JSON tools hub for the full debugging workflow.