"category": "inspect-and-test",

JSON Schema Generator

"tldr": Paste a JSON document and get a JSON Schema (draft 2020-12) - types inferred per field, nested objects and arrays included.

Generate a JSON Schema (draft 2020-12) from a sample document. The generator infers types for every field - distinguishing integer from number - recurses into nested objects and arrays, and lists every observed property as required.

{"json": "json schema"}

A schema turns implicit assumptions about a payload into an enforceable contract: validate API requests before processing, catch breaking changes in CI, and power editor autocomplete for config files.

How to generate a JSON Schema

  1. 1Paste a representative JSON document - the more complete, the better the schema.
  2. 2Types are inferred per field; whole numbers become "integer", decimals become "number".
  3. 3Every property present in the sample is marked required - relax this for optional fields.
  4. 4Copy the schema and validate with ajv (JavaScript), jsonschema (Python), or your framework.

Convert JSON to JSON Schema in code

Validate with ajv
import Ajv from "ajv"; // npm install ajv

const ajv = new Ajv();
const validate = ajv.compile(schema);
if (!validate(data)) {
  console.error(validate.errors); // path, keyword, message per failure
}
Validate with jsonschema
from jsonschema import validate, ValidationError  # pip install jsonschema

try:
    validate(instance=data, schema=schema)
except ValidationError as e:
    print(e.json_path, e.message)

Frequently asked questions

Every field is listed as required - is that right?

The generator can only observe one sample, where every field was present. Edit the required array down to the fields that are truly mandatory - that is usually the only manual adjustment a generated schema needs.

Which schema draft is generated and does it matter?

Draft 2020-12, the current standard, declared in the $schema field. Most validators (ajv 8+, python-jsonschema 4+) support it fully; for tools stuck on draft-07, changing the $schema URI is usually sufficient since these basic keywords are identical.

How do I express that a field can be null?

Use a type array: {"type": ["string", "null"]}. If your sample had null for a field, the generator types it as null only - update it to the union of real types.

Can I add constraints like string length or number ranges?

Yes - the generated schema is a starting skeleton. Add minLength/maxLength, pattern, minimum/maximum, enum, and format ("email", "uri", "date-time") to encode the real business rules. Validators enforce them all.

Last updated:

You might also need

Free and in-browser, like everything on JSON Console. Browse all tools →