Skip to content

JSON to JSON Schema Generator - Draft 2020-12

Input JSON
JSON Schema Output

A first draft of the schema

Writing a JSON Schema from nothing means listing every property and type by hand. If you already have example payloads, this does the first pass: paste the JSON and it writes a schema that the sample passes. Then you tighten it. It uses the same type inference as our code generators, so array items are merged into one shape and the required list comes from what was actually present.

Example

Input, with the schema title set to UserList:

{
  "users": [
    { "id": 1, "email": "[email protected]", "joined": "2024-03-01T09:30:00Z", "tags": ["admin"] },
    { "id": 2, "email": null, "joined": "2024-05-12T14:02:00Z", "tags": [], "manager": 1 }
  ]
}

Output with the default options:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "UserList",
  "type": "object",
  "properties": {
    "users": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/User"
      }
    }
  },
  "required": ["users"],
  "$defs": {
    "User": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "email": {
          "type": ["string", "null"],
          "format": "email"
        },
        "joined": {
          "type": "string",
          "format": "date-time"
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "manager": {
          "type": "integer"
        }
      },
      "required": ["id", "email", "joined", "tags"]
    }
  }
}

manager was only on the second user, so it isn't in required. email is required but nullable. The empty tags array on the second user didn't erase the string item type learned from the first.

Options

Draft switches between 2020-12 and draft-07. Named $defs puts each nested object under $defs (definitions in draft-07) and points to it with $ref; untick it to get one nested schema with everything written inline, which some form generators and LLM structured-output APIs handle better. Detect formats adds format for strings that look like date-time, date, email, uri or uuid. A format is only kept if every sample of that field matched it, so one free-text value drops it. Unticking Allow extra properties adds "additionalProperties": false to every object.

Using it with OpenAPI and validators

OpenAPI 3.1 uses 2020-12 as its schema dialect, so you can paste the objects under $defs into components/schemas and change each $ref from #/$defs/User to #/components/schemas/User. OpenAPI 3.0 is different: it doesn't accept type arrays, so "type": ["string", "null"] has to become "type": "string", "nullable": true. The generator doesn't do that rewrite for you.

In code, Ajv takes the schema as-is: new Ajv2020().compile(schema) for 2020-12, or the default Ajv export for draft-07, plus addFormats(ajv) if you kept the formats. In Python, jsonschema.Draft202012Validator(schema).iter_errors(doc) lists every problem.

What to add by hand

Fields with values of different types become a type array such as ["integer", "string"], or an anyOf when one of them is an object or array. Fields that were null in every sample get {}, which accepts anything, so search for those first. After that, add the rules a sample can't show: enum for status fields, minimum for quantities, pattern for IDs, and description text for anyone reading the schema later.

To check documents against the result, paste it into the JSON Schema validator. If the consumer is TypeScript code, JSON to Zod gives you a runtime validator from the same sample, and JSON to TypeScript gives you the static types.

Same JSON, other languages

Every generator here reads the sample the same way, so a key that is optional or nullable in one language is optional or nullable in the others. Paste the same input into: TypeScript, Zod, Go, Python, Rust, Java, C#.

Related Tools

Frequently Asked Questions

Should I pick draft 2020-12 or draft-07?
2020-12 if your validator supports it: Ajv, Python's jsonschema and recent OpenAPI 3.1 tooling all do. Pick draft-07 for older libraries and for tools that stop there. For what this generator outputs, the only difference is the `$schema` URI and `$defs` versus `definitions`.
How does it decide which properties are required?
A key is required when it appears in every object of that shape across the sample. With one record, everything is required. Paste an array of several real records and keys that are sometimes missing drop out of `required`.
How are null values written?
As a type array, for example `"type": ["string", "null"]`, when the field had a value in one record and null in another. A `$ref` that can be null is written as `anyOf` with `{ "type": "null" }`, because a `$ref` and a type array can't share one schema.
Are formats like email enforced?
In 2020-12, `format` is an annotation unless the validator opts in to checking it. Ajv checks formats when ajv-formats is installed, and our JSON Schema validator does that. Untick Detect formats if your strings only look like dates or URLs by accident.
Does it infer enums, minimum or maxLength?
No. A few samples can't tell you the full set of allowed values or the real limits, and guessing would reject valid data. Add `enum`, `minimum` or `maxLength` by hand where you know the rule.