JSON to JSON Schema

Generate a JSON Schema from JSON.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What JSON to JSON Schema does

JSON to JSON Schema creates a JSON Schema (draft-07) that describes the structure of a sample JSON document: the type of every value, the properties of every object and the item type of every array. It is a starting point for request validation or documentation, to be tightened by hand.

JSON to JSON Schema examples

The built-in sample

Input

{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}

Output

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "active": {
      "type": "boolean"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "address": {
      "type": "object",
      "properties": {
        "city": {
          "type": "string"
        }
      },
      "required": [
        "city"
      ]
    }
  },
  "required": [
    "name",
    "active",
    "tags",
    "address"
  ]
}

This is the input that the Load sample button fills in.

Awkward keys, a null, a decimal and arrays

Input

{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}

Output

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "order-id": {
      "type": "integer"
    },
    "total": {
      "type": "number"
    },
    "paid": {
      "type": "boolean"
    },
    "coupon": {
      "type": "null"
    },
    "items": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "sku": {
            "type": "string"
          },
          "qty": {
            "type": "integer"
          }
        },
        "required": [
          "sku",
          "qty"
        ]
      }
    },
    "tags": {
      "type": "array",
      "items": {}
    }
  },
  "required": [
    "order-id",
    "total",
    "paid",
    "coupon",
    "items",
    "tags"
  ]
}

The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.

Each output was produced by running this tool on the input above it.

How it works

The sample is walked recursively. Each object becomes {"type":"object"} with a properties entry per key, and every key seen in the sample is listed in required. Arrays use the schema of their first item for items; an empty array gets an empty items schema, which accepts anything. Whole numbers become integer, other numbers number, and null values {"type":"null"}.

Review the result: remove optional fields from required, and widen null types (for example to ["string","null"]) where a field can hold a value. Unlike the code generators, the schema generator looks only at the first item of an array, so a field that appears only in later items is missing from the schema.

Details and limits

  • Values are carried across without losing anything in ordinary data; the points above list the cases the two formats handle differently.
  • The schema declares draft-07 in $schema. It contains only type, properties, required and items: no formats, patterns, enums or length limits are guessed from the sample.
  • additionalProperties is not set, so the schema accepts objects with extra keys.
  • A top-level array gives a schema of type array; a top-level string or number gives a schema with just that type.
  • Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.

Questions

Is my data uploaded when I use JSON to JSON Schema?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does JSON to JSON Schema do?

JSON to JSON Schema creates a JSON Schema (draft-07) that describes the structure of a sample JSON document: the type of every value, the properties of every object and the item type of every array. It is a starting point for request validation or documentation, to be tightened by hand.

How does JSON to JSON Schema work?

The sample is walked recursively. Each object becomes {"type":"object"} with a properties entry per key, and every key seen in the sample is listed in required.

Are there any limits I should know about?

The schema declares draft-07 in $schema. It contains only type, properties, required and items: no formats, patterns, enums or length limits are guessed from the sample. additionalProperties is not set, so the schema accepts objects with extra keys.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.