AI

How to Generate a JSON Schema

Dev Nexus5 min read

Learn how to turn a sample JSON document into a JSON Schema, then refine the inferred types and required fields into a contract you can validate against.

You have a JSON payload — an API response, a config file, a record from your database — and you need a JSON Schema that describes it. Maybe you want to validate incoming requests, document a data contract, or wire up editor autocompletion. Writing that schema by hand, key by key, is slow and easy to get wrong.

The faster path is to generate the schema from a real example. This guide walks you through taking a sample JSON document, inferring a schema from it, and then refining that draft into a contract strict enough to trust in production.

The Problem

Hand-authoring a JSON Schema means opening the spec in one tab and your data in another, then translating every field by hand. For a flat object with five keys that's tedious; for a deeply nested API response it's a genuine time sink and a magnet for mistakes.

The errors are subtle. You forget a field, mistype a property name so it never matches, declare a number where the data is actually a string, or lose track of which keys are required. A schema with any of these bugs is worse than none — it passes data it should reject, or rejects data it should accept, and you don't find out until something breaks downstream.

Worse, the schema has to keep pace with the payload. Every time the API adds a field, someone has to remember to update the hand-written schema too. In practice, they often don't.

The Solution

Generating the schema from a sample flips the work around: instead of describing the data from memory, you let the structure of a real document drive the schema.

A generator parses your JSON and walks the tree, recording each value's type. Objects become properties blocks, arrays get an items definition inferred from their elements, and nested structures are described recursively. In one paste you get a complete, accurate draft that already mirrors your real data.

The JSON Schema Generator does this entirely in your browser — your sample is never uploaded, so you can safely generate schemas from production responses and private records. From there you refine: mark required fields, tighten types, and add constraints. Below is the full workflow, from raw sample to strict contract.

Step-by-Step Guide

  1. 1

    Choose a complete sample

    Pick a JSON document that includes every field you want in the schema. The generator can only describe what it sees, so if a field is sometimes present, use an example that has it. A single rich record beats a stripped-down one — the more complete your sample, the less you'll have to add by hand afterwards.

  2. 2

    Paste it and generate

    Open the JSON Schema Generator and paste your sample into the input. Minified or pretty-printed JSON both work. Click Generate, and the tool parses the document and infers a schema — types for each value, property blocks for objects, and item definitions for arrays. If the paste is easier to read formatted first, run it through the JSON Formatter.

  3. 3

    Review the inferred types

    Read the generated schema against your knowledge of the data. Inference is literal: a whole number becomes number, not integer; a date string becomes string, not a date-time format. Anywhere the sample under-describes reality, note it — you'll tighten these next.

  4. 4

    Mark required fields and tighten constraints

    Inference can't know which fields are mandatory, so add a required array listing the keys your system depends on. Then narrow types: change number to integer where fractions are invalid, add format (like email or date-time), enum for fixed value sets, and bounds like minimum or maxLength. This is what turns a descriptive draft into a strict contract.

  5. 5

    Copy the schema and validate against it

    Copy the finished schema into your project and point a validator at it — Ajv in JavaScript, jsonschema in Python, or your API gateway. Run a few known-good and known-bad payloads through it to confirm it accepts and rejects the right things before you rely on it.

Common Mistakes

  • Generating from an incomplete sample

    If your sample is missing optional fields, the schema won't mention them, and a strict validator may reject data that's actually valid. Use the most complete record you have, or merge fields from several samples so the schema covers every case.

  • Shipping the inferred schema unedited

    A generated schema describes one example — it doesn't know which fields are required or how tight your constraints should be. Treat the output as a first draft. Skipping the refinement step gives you a schema that validates almost nothing.

  • Trusting types inferred from null

    A field that's null in the sample has no inferable type, so the generator can't tell whether it's really a string, number or object. Always set the correct type for null fields after generating, or the schema will misdescribe them.

  • Forgetting to update the schema when the payload changes

    A schema is only useful while it matches the data. When the API adds or renames a field, regenerate from a fresh sample and re-apply your constraints, rather than letting the schema drift out of sync.

Frequently Asked Questions

How do I generate a JSON Schema from a JSON document?

Paste a representative sample into a schema generator. It parses the JSON, walks the structure, and infers types, properties and array items into a draft schema. With Dev Nexus this happens in your browser, so the sample is never uploaded.

Does generating a schema from one sample cover every case?

Not on its own. A single sample can only show the fields it contains, and it can't say which are required. Use a complete example and then add required fields and tighter constraints to make the schema strict.

Which JSON Schema draft should the generated schema use?

Match whatever your validator expects — Draft 7 is widely supported, and 2020-12 is the newest. The generator produces standard keywords; you set the $schema declaration to the draft your tooling reads.

Why is my number field typed as number instead of integer?

Inference is literal: any JSON number reads as type number, since the value alone doesn't reveal whether fractions are allowed. If a field must be a whole number, change its type to integer after generating.

Is it safe to generate a schema from production JSON?

With a browser-based tool like the Dev Nexus JSON Schema Generator, yes — your sample is parsed and inferred locally and never sent to a server, so tokens and personal data stay on your machine.

Try the Tool

JSON Schema

Paste a sample JSON document and get an accurate JSON Schema draft in seconds.

Open JSON Schema

Related Tools

Related Articles