Skip to content

JSON Schema Generator

This JSON Schema generator turns a sample JSON document into a draft 2020-12 schema, ready to copy and tighten.

Input · Sample JSON
Output · JSON Schema
Valid JSONRuns in your browser

Load from a URL

Your browser fetches it directly, so the server must allow cross-origin requests (CORS).

How to generate a JSON Schema

  1. Paste a sample into Sample JSON. The schema appears as you type.
  2. Set Title. It becomes the schema’s title in PascalCase: order item turns into OrderItem.
  3. Download saves a .json file named after the title.

A syntax error in the sample shows its line and column in place of the schema.

Example

With Title set to Order:

{
  "orderId": 7731,
  "items": [
    { "sku": "BAG-07", "qty": 1, "price": 54, "note": null },
    { "sku": "SOCK-3P", "qty": 2, "price": 12.5, "note": "gift wrap", "discount": 0.1 }
  ],
  "coupon": null
}

Generates:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Order",
  "type": "object",
  "properties": {
    "orderId": {
      "type": "integer"
    },
    "items": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "sku": {
            "type": "string"
          },
          "qty": {
            "type": "integer"
          },
          "price": {
            "type": "number"
          },
          "note": {
            "type": [
              "string",
              "null"
            ]
          },
          "discount": {
            "type": "number"
          }
        },
        "required": [
          "sku",
          "qty",
          "price",
          "note"
        ],
        "additionalProperties": false
      }
    },
    "coupon": {}
  },
  "required": [
    "orderId",
    "items",
    "coupon"
  ],
  "additionalProperties": false
}

From the two items:

  • price is a number: 54 alone gives integer, but 12.5 widens it.
  • note allows ["string", "null"]: null in one item, text in the other.
  • discount is not required: only the second item has it.
  • coupon is {}, which accepts anything, because the sample only shows null. Put the real type there.

An empty array becomes "items": {}, which accepts any item. A field that is a number in one item and a string in another becomes an anyOf of both.

Every object gets "additionalProperties": false, so unknown fields fail. Remove it where an API may add fields.

What a JSON Schema generator cannot infer

One sample shows types, not rules. A timestamp and an email address are both "type": "string", so add "format": "date-time" or "format": "email" yourself, plus minimum, maximum, pattern and enum where they apply. Nested objects are written inline rather than as shared $defs, even when two match.

Test it

Check the schema in the JSON Schema validator against real documents, including some that should fail. For types instead, JSON to TypeScript and JSON to Zod read the same sample; Zod also validates at runtime.

Questions

Can I generate one schema from several samples?

Yes. Put the samples in one array and paste that. The items are merged: a field missing from some becomes optional, and a field that is sometimes null becomes nullable. Then use the items part of the result as your schema.

How do I make a generated field optional or nullable?

To make it optional, remove its name from the required list of its object. To allow null, change "type": "string" to "type": ["string", "null"].

Can it create sample JSON from a schema?

No. It works in one direction only, from data to schema.

Related tools

Esc