Skip to tool

JSON Schema Generator

Input JSON
Schema

The inferred JSON Schema

Generate a JSON Schema from JSON

The JSON Schema Generator takes one sample JSON document and produces a draft-07 JSON Schema describing its shape: the type of every value, the properties of every object and the item type of every array.

The result is a starting point: it records only what the sample shows, which is usually stricter than what your data allows. Edit it by hand before using it for validation.

How types are inferred

The input is parsed with JSON.parse and walked recursively. Strings map to string, true and false to boolean, null to null, objects to object and arrays to array. Numbers without a fractional part become integer; other numbers become number. The top level gets $schema set to http://json-schema.org/draft-07/schema# and title set to the Root type name.

For an object, every key is listed under properties and under required, and additionalProperties is false. The schema therefore rejects documents that lack a sample key or add a new one.

For an array, each element's schema is inferred and merged into one items schema. When all elements are objects, a key is required only if every element has it; the rest become optional properties. When elements differ in type, items gets a type list such as ["integer", "string"]. An empty array gives {"type": "array"} without items.

Example: a product record

Input, with Root type name set to Product: {"id": 7, "price": 19.5, "tags": ["sale", "new"], "owner": {"name": "Ada", "email": null}, "variants": [{"sku": "A1", "stock": 3}, {"sku": "B2"}]}

The output has title Product, type object, all five keys required and additionalProperties false. id is integer and price is number. tags is an array of string. owner is an object with name (string) and email (null), both required.

In variants, the merged items schema has properties sku (string) and stock (integer), but only sku is required, because the second element has no stock.

Before using it you would likely change email to ["string", "null"], since the sample happened to be null.

Limits of inferring from one sample

  • Only draft-07 is produced; 2019-09 and 2020-12 cannot be selected.
  • Only type, properties, required, additionalProperties and items are generated. Formats (email, date-time, uri), patterns, enums, ranges and length limits are not detected.
  • A field that is null in the sample is typed as null only.
  • Every key is required and extra properties are forbidden. Loosen this if real data can omit or add fields.
  • When objects are mixed with other types in an array, for example with null, the object's properties are dropped from items. Mixed integers and decimals give ["integer", "number"] instead of number.
  • No $ref or definitions are generated; repeated structures are written out in full.

JSON Schema questions

What is JSON Schema used for?

It is a JSON document stating what valid data looks like. Libraries such as Ajv and Python's jsonschema validate data against it, OpenAPI uses a variant of it for request and response bodies, and editors such as VS Code use it for completion in configuration files.

Why is 1.0 typed as integer?

JSON.parse turns 1.0 into 1, so the generator cannot tell them apart. In draft-07, integer means any number with a zero fractional part, so 1.0 validates as integer anyway. If the field can hold decimals, change it to number.

Why does an items schema contain a list of types?

The array holds more than one JSON type. If the list is ["integer", "number"], replace it with number, which already accepts integers.

How do I validate JSON against the generated schema?

Copy the schema into a file and use a draft-07 validator. With ajv-cli: ajv validate -s schema.json -d data.json. In Python: from jsonschema import validate; validate(instance=data, schema=schema), which raises ValidationError on the first mismatch. This page generates the schema but does not validate other documents against it.

How can I get a more accurate schema?

Use a sample with optional fields, several array elements and real values instead of null.

Can I use the generated schema with draft 2020-12 or OpenAPI?

Yes, with small edits. The keywords it emits (type, properties, required, additionalProperties and items) mean the same in draft 2020-12, so a 2020-12 validator only needs $schema changed to https://json-schema.org/draft/2020-12/schema. OpenAPI 3.1 schemas are 2020-12. OpenAPI 3.0 uses an older subset that does not allow a list in type, so replace a type list such as ["string", "null"] with one type plus nullable: true.

References