How to Generate JSON Schema from JSON
Use a JSON Schema generator to create a schema from sample JSON - choose the draft, infer types, mark required fields, and add examples or an $id.
JSON tells you what data you have. JSON Schema tells other tools what data you expect. A schema can validate API payloads, document config files, power forms, and protect pipelines from bad input. This guide shows how to use a JSON Schema generator well, and what to check after generating a schema from sample JSON.
The quickest way: generate JSON Schema online
Paste a representative JSON sample into the JSON Schema Generator, choose a draft, decide how required fields should work, and generate. The tool infers object properties, arrays, numbers, booleans, strings, nulls, and nested structures in your browser.
Use a client-side JSON Schema generator when your sample contains real API payloads or private config. The schema can be generated without uploading your data.
Use it when: you need a strong first draft of a JSON Schema without writing every property by hand.
Pick the right JSON Schema draft
JSON Schema has several drafts. The common choices are:
- draft-07 - still widely supported by older validators and tools.
- 2019-09 - newer vocabulary and validation behavior.
- 2020-12 - the current modern option for many new projects.
The best draft is usually the one your validator supports. If you are using an existing project, check its validation library first. If you are starting fresh, 2020-12 is a good default.
Generate from a representative sample
A generator can only infer what it sees. This sample:
{ "id": 1, "email": "ada@example.com" }
can produce:
{
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string" }
},
"required": ["id", "email"]
}
But it cannot know whether email is always required, whether id can be a string, or whether there are more fields in real data. Use several realistic examples when possible.
Required fields need a decision
Required fields are the most important setting. There are two common approaches:
- All keys required - good for strict config files and stable API contracts.
- Only always-present keys required - better when generating from an array of sample objects.
If one object in your sample lacks a field, that field is probably optional. A good generator can infer that from array samples.
Types: integer vs number, nulls, and arrays
Good schema generation should preserve useful distinctions:
1should beinteger;1.5should benumber.trueandfalseshould beboolean.nullshould be included when a value can be null.- arrays should include an
itemsschema. - nested objects should produce nested
properties.
For mixed arrays, the generated schema may need anyOf or a wider type. Review those by hand, because mixed data can mean either a flexible API or a messy sample.
Add metadata for humans and tools
A useful schema is not just validation rules. Add:
- $id - a stable identifier for the schema.
- title - a readable schema name.
- description - what the object represents.
- examples - sample values for documentation and form builders.
Generated schemas are a strong start, but a short description on important fields makes them far easier to maintain.
Validate with the generated schema
After generation, test the schema against both valid and invalid examples. In JavaScript, many teams use a validator in this shape:
import Ajv from "ajv";
const ajv = new Ajv();
const validate = ajv.compile(schema);
if (!validate(data)) {
console.log(validate.errors);
}
The schema is only useful if it rejects bad data without blocking good data, so run a few real payloads through it before committing.
Which method should you use?
For a fast first draft, use the online JSON Schema Generator: paste JSON, pick the draft, set required-field behavior, and copy the schema. Then review required keys, null handling, array items, and metadata. For API platforms, connect that schema to your validator or documentation workflow.
Need TypeScript too? See how to convert JSON to TypeScript. Need to check raw syntax first? Use the JSON Validator or read how to validate JSON.
Ready to merge your JSON?
Combine files or snippets in your browser — free and private.
Open the merge toolKeep reading