Skip to content

JSON to JSON Schema

Infers a JSON Schema from one or several samples, unioning the shapes it sees.

Describes a sample document as a JSON Schema (2020-12): types, properties, `required`, and nested `$defs` for repeated shapes. Useful for a contract test, an OpenAPI component, or anywhere a validator wants a schema rather than an example.

🔒 Runs entirely in your browserjson-schemajsoncodegenvalidation
Emit

Sample JSON

Drop a .json file

Read in this tab. Nothing is uploaded.

Give it every element you have. Every extra record narrows the guess — a field missing from one of them is what makes it optional.

TypeScript

How to use it

  1. Paste as many records as you have

    Every element of every array is read and merged, not just the first. Three records tell it far more than one, because a field missing from one of them is exactly what makes that field optional.

  2. Name the root type

    Nested objects are named after the key that holds them, singularised for arrays, so `users` yields a `User`. Only the root needs a name from you.

  3. Read the counts under the output

    Types, optional fields, unions and how many records were merged. A union count above zero means a field held two different kinds of value across your sample — worth looking at before you paste the result into a codebase.

  4. Take the schema to a validator

    The output carries its `$schema` keyword, so it can go straight into the JSON Schema validator on this site or into any 2020-12 implementation.

Common questions

Which draft is this?

Draft 2020-12, declared in the `$schema` keyword. Reusable shapes go in `$defs` and are referenced with `$ref`, which is that draft’s spelling; Draft-07 tooling wants `definitions` instead and will not resolve these references.

Why is nothing constrained beyond its type?

No `format`, `minLength`, `pattern` or `enum` is emitted. Every one of those would be inferred from a handful of records, and a schema that rejects valid production data is worse than one that is merely loose. Add the constraints you can actually justify.

Is `additionalProperties` set?

It is left open, which means extra keys validate. Closing it would turn every field the API adds later into a validation failure, and a sample cannot tell you whether the shape is meant to be closed.

How does it decide what goes in `required`?

A property is required when it was present in every record that was merged into that shape. Paste one record and everything is required; paste five and the schema starts telling you something real.

Feed it from

Next

These accept what this tool produces, so the result can go straight in.