JSON → JSON Schema

JSON input
Loading editor...
JSON Schema outputLive
Loading editor...
// Generated JSON Schema code will appear here...

What is JSON → JSON Schema?

Generate a JSON Schema from example JSON. Paste a sample document, or an array of samples, and get a schema that describes its types, required properties, nullable fields and nested objects and arrays.

String formats are detected automatically when every sample matches: date-time, date, email, uuid and uri. Values that can be null get a type list such as ["string", "null"], and arrays with mixed item types use anyOf.

Choose draft 2020-12 or draft-07, set a title, decide whether keys found in every sample are required, and optionally forbid additional properties. When you paste a top-level array, its items are merged into one object schema, so keys missing from some samples become optional.

Use the schema to validate API requests and responses, document a payload, power editor autocompletion, or generate types. Everything runs in your browser.

Features

  • Draft 2020-12 or draft-07 output
  • Types inferred for strings, integers, numbers, booleans, null, objects and arrays
  • Required properties from keys present in every sample
  • Nullable fields as type lists, mixed values as anyOf
  • Format detection: date-time, date, email, uuid, uri
  • Optional additionalProperties: false on every object
  • Merge many samples into one schema
  • 100% client-side: nothing is uploaded

How to use JSON → JSON Schema

  1. Paste sample JSON. Enter one representative document, or an array of documents to merge.
  2. Set the options. Open Settings to choose the draft, a title, required properties, additional properties and format detection.
  3. Refine and use. Copy the schema, then add constraints a sample cannot show, such as enum, minimum or pattern.

Examples

Object to JSON Schema 2020-12

Title set to User. The uuid and email formats are detected from the values.

Input

{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "name": "Ada",
  "email": "ada@example.com",
  "tags": [
    "admin"
  ],
  "manager": null
}

Output

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "manager": {
      "type": "null"
    }
  },
  "required": [
    "id",
    "name",
    "email",
    "tags",
    "manager"
  ]
}

Merging samples (draft-07)

A top-level array is merged into one object schema. age is missing from one sample, so it is not required. Additional properties are turned off.

Input

[
  {
    "name": "Ada",
    "age": 36
  },
  {
    "name": "Bob"
  }
]

Output

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  },
  "required": [
    "name"
  ],
  "additionalProperties": false
}

Frequently asked questions

What is JSON Schema?

JSON Schema is a standard vocabulary for describing the structure of JSON data: which properties exist, their types, which are required, and further constraints. Validators in most languages use it to check data, and tools use it for documentation and code generation.

Which draft should I choose?

Use 2020-12, the current version, unless a tool you rely on only supports draft-07. OpenAPI 3.1 uses 2020-12, while many older validators and editors expect draft-07.

How are required properties decided?

A property is required when it appears in every sample, even if its value is null. Paste several samples as an array to get a more accurate result, or set Required properties to None.

Can I generate a schema from multiple JSON examples?

Yes. Paste them as a top-level array and keep Merge top-level array items on. Types are combined, and keys missing from some samples become optional.

Does the generated schema include enums or ranges?

No. A sample cannot show which values are allowed, so add enum, minimum, maximum, pattern or length limits yourself after generating.

Is my data uploaded?

No. The schema is generated entirely in your browser.