OnlineTextLab
Esc

JSON to JSON Schema Generator

Writes a draft 2020-12 or draft 7 schema from sample JSON, merging list items so required keys and types are accurate.

JSON Schema output

Your JSON Schema appears here as you type.

Nothing to paste yet? Use Try an example to see it work.

Options
Draft

Runs on your device. Your files and text are not uploaded.

About JSON to JSON Schema

Writing a JSON Schema by hand for a large object is tedious, and most of the work is mechanical: listing the properties, naming their types, marking what is required. Generating that first draft from real data leaves you with the interesting part, which is tightening the rules.

This is useful when you are adding request validation to an API, describing a configuration file so editors can autocomplete it, or writing a contract test against someone else's service.

The output is a complete schema document with $schema and title. It will accept the sample you gave it, and you should expect to refine it, because a sample cannot express every rule.

How it works

All values are merged into one shape before anything is written. So items describes every element of a list, not only the first, and a key belongs in required only if every object at that spot has it. A key that is present in some list items and not others is still listed under properties.

Numbers are integer when every value is whole and number if any is not. A field seen as several types gets a list, such as ["string", "null"]. An empty list gets items: {}, which allows anything.

With Detect string formats on, format is added only when every string at that position matches: email, uri, uuid, date or date-time. Note that the 2020-12 specification treats format as an annotation by default, so a validator may not enforce it unless configured to.

How to use JSON to JSON Schema

  1. Paste a representative JSON document, or use Open file.
  2. Give it a Schema title and pick the Draft your validator supports.
  3. Decide on required keys, string formats and rejecting unknown keys under Options.
  4. Get the schema with Copy or Download .json.

An order with an optional field in its list items

Input
{"order_id":1042,"email":"[email protected]","lines":[{"sku":"PEN-1","qty":2},{"sku":"INK-9","qty":1,"note":"gift"}]}
Output
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Order",
  "type": "object",
  "properties": {
    "order_id": {
      "type": "integer"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "lines": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "sku": {
            "type": "string"
          },
          "qty": {
            "type": "integer"
          },
          "note": {
            "type": "string"
          }
        },
        "required": [
          "sku",
          "qty"
        ]
      }
    }
  },
  "required": [
    "order_id",
    "email",
    "lines"
  ]
}

This run has Detect string formats on. note appears in only one line item, so it is in properties and left out of required, and email picked up its format.

When it helps

  • Starting request validation for an API endpoint from a real payload.
  • Giving a JSON configuration file autocomplete and checking in an editor.
  • Writing a contract test that notices when a partner's response changes shape.
  • Documenting a data format for another team.

Tips for a better result

  • Use a list of several real objects as your sample, so that required keys are not overstated.
  • Add enum, minimum, maxLength and pattern by hand afterwards. A sample cannot reveal those limits.
  • Reject unknown keys adds additionalProperties: false everywhere. That catches typos, but it will also fail when the other side adds a harmless new field.
  • Check which draft your validator supports. Draft 7 remains widely supported, and for the keywords generated here only the $schema line differs.

Good to know

  • If your sample is a list, the schema describes the list, with type: "array" at the root. Paste a single object for an object schema.
  • No $ref or $defs are produced, so repeated structures are written out in full.
  • Tuples are not detected. A list holding a number followed by a string is described as a list of either.
  • Only the five named string formats are recognized, and a coincidental match in a small sample can add one you do not want.

Questions people ask

Which draft should I choose?

Use 2020-12 for new work if your validator supports it, as it is the current release. Choose draft 7 for older tooling. Only the $schema line changes in the output here, as the keywords used are common to both.

Why is a key missing from required?

At least one object at that position in your sample does not have it, so the schema treats it as optional. Turn off the required option if you prefer to add these by hand.

Does the generated schema validate my sample?

It should. We checked generated schemas against their samples with an independent validator, including with Reject unknown keys on. The one possible exception is format, where a strict validator could disagree with the patterns used for detection.

Why do I see integer and not number?

Every value at that position is a whole number. If a price could be 1.5, include such a value in your sample, or change the type by hand.

What to use next

Learn more

Last reviewed . Read our editorial policy.