JSON to JSON Schema
Instantly convert JSON to JSON Schema draft-07. Infer types, required fields, arrays, and nested objects from sample JSON. Free, browser-based, no sign-up.
- Runs in your browser
- Your data never leaves your browser
- Free · No Sign-Up
Scan with WeChat to share this tool
Examples, details and FAQ Worked examples, how it compares with other tools, and answers to common questions.
JSON to JSON Schema Online
JSON Schema is a vocabulary that describes the structure of JSON data: expected types, required fields, format rules and the shape of nested objects. Validators such as Ajv check data against it, and it is used for API validation, code generation and documentation.
Paste a representative JSON object or array and ZeroTool will infer a draft-07 JSON Schema with object properties, required fields, array item types, and nested structures. This is a fast starting point for API validation, documentation, mocks, contract tests, and data pipeline checks.
If you searched for to_json_schema or jinja2schema infer, note the difference:
template inference tools inspect Jinja templates, while this generator infers a schema from concrete JSON examples.
Understanding the Output
The generated schema always includes $schema pointing to draft-07.
Object properties are inferred from the JSON keys.
The required array lists every non-null key found.
Array items use an inferred schema based on the elements present.
Next Steps After Generation
A generated schema is a starting point. Common refinements include: adding
description fields for documentation, tightening string constraints
with minLength or pattern, marking optional properties by
removing them from required, and adding enum for fields
with a known set of values.
Example
Input:
{
"orderId": 1001,
"total": 59.9,
"coupon": null,
"items": [{ "sku": "A1", "qty": 2 }],
"tags": []
}
Output:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"orderId": { "type": "integer" },
"total": { "type": "number" },
"coupon": { "type": "null" },
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"sku": { "type": "string" },
"qty": { "type": "integer" }
},
"required": ["sku", "qty"]
}
},
"tags": { "type": "array", "items": {} }
},
"required": ["orderId", "total", "items", "tags"]
}
- A whole number becomes
integerand a decimal becomesnumber. Ifqtycan be1.5, change it tonumber. couponisnullin the sample, so its type isnulland it is not inrequired. If it can also be a string, change the type to["string", "null"].- An empty array gives
"items": {}, which accepts any item.
Example: Success and Error Responses
An API that returns {"ok": true, "data": …} on success and {"ok": false, "error": …} on failure needs both shapes in the sample. Put one of each in an array:
[{"ok": true, "data": {"id": 7}}, {"ok": false, "error": "not found"}]
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"ok": { "type": "boolean" },
"data": { "type": "object", "properties": { "id": { "type": "integer" } }, "required": ["id"] },
"error": { "type": "string" }
},
"required": ["ok"]
}
}
ok appears in both responses, so it is the only required key; data and error are optional. Use the items schema to validate a single response. A sample with only the success response would make data required and reject every error response.
Using the Schema with Ajv
Ajv 8 validates draft-07 with its default Ajv class. The format keyword needs the ajv-formats package; without it, compile() throws unknown format "email":
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
const ajv = new Ajv();
addFormats(ajv);
const validate = ajv.compile({
type: 'object',
properties: {
email: { type: 'string', format: 'email' },
coupon: { type: ['string', 'null'] },
},
required: ['email'],
additionalProperties: false,
});
validate({ email: 'a@example.com', extra: 1 }); // false: must NOT have additional properties
The generator does not add format, minimum, maxLength, pattern, enum, or additionalProperties: false. Add them for the rules your data must follow.
Limits
- Array items are merged. For
[{"id": 1, "name": "Alice"}, {"id": 2, "email": "b@example.com"}],itemslistsid,name, andemail, and onlyid, which every item has, is required. Integers and decimals in one array become"number". The schema always accepts the sample it came from; GenSON merges samples the same way. - Required comes from the sample. In a single object every non-null key is required. If a field is optional in your data but present in the sample, remove it from
requiredyourself. - Mixed types give a type list. An array such as
[1, "x", {"a": 1}]gives"type": ["integer", "string", "object"]with the object’spropertiesandrequirednext to it. Those keywords apply only to the object items. - Draft-07 only. To use draft 2020-12, change
$schemaand use Ajv’sAjv2020class.
FAQ
Which JSON Schema version does this tool generate?
This tool generates JSON Schema draft-07, one of the most widely supported versions, compatible with validators like AJV, jsonschema (Python), and the JSON Schema Validator tool on ZeroTool.
How are required fields determined?
Every non-null property found in the input JSON object is added to the required array. Null values are excluded because their presence cannot be guaranteed. In an array of objects, a key is required only when every object has it with a non-null value. You can edit the output schema to adjust the required list for your use case.
Does it handle nested objects and arrays?
Yes. The tool recursively processes nested objects and arrays. Arrays get one items schema merged from all their elements, so every element passes it. Nested objects produce nested properties and required arrays.
Can I use the generated schema for validation?
Yes. Paste the generated schema into the JSON Schema Validator tool on ZeroTool or any AJV-based validator. You may need to refine optional fields or add additional constraints like minLength, minimum, or pattern.
Is my JSON sent to a server?
No. The schema is generated in your browser tab as you type, and the JSON is not sent to a server or saved in browser storage. When you finish editing the input or copy the schema, the page's analytics records one usage event with the tool name and the action (convert or copy), not the JSON or the schema.
Can this replace jinja2schema infer().to_json_schema()?
Use jinja2schema when you need to infer data requirements from Jinja templates in Python code. Use ZeroTool when you have example JSON and want to infer a draft-07 JSON Schema quickly in the browser, without installing a package.