JSON Schema Generator - Generate Schema from JSON Data Online
Generate JSON Schema from JSON data for API validation, documentation, and code generation. Supports Draft-04, Draft-07, and 2020-12 — 100% client-side, no data sent to server.
Frequently Asked Questions
What is a JSON Schema Generator?
A JSON Schema Generator is a tool that automatically creates a JSON Schema definition from sample JSON data. JSON Schema is a standard vocabulary (defined at json-schema.org) that describes the structure, types, and constraints of JSON documents. Instead of manually writing schema definitions, you simply paste a JSON example and the tool infers the correct schema including object properties, data types, required fields, array item types, and string formats like dates and emails.
How do I generate JSON Schema from my JSON data?
Paste your JSON data into the input field on the left side. The tool instantly generates a JSON Schema in the output panel on the right. You can customize the output by selecting a schema draft version (Draft-04, Draft-07, or 2020-12), toggling required fields detection, enabling string format detection for patterns like dates and emails, and controlling whether additional properties are allowed. Once satisfied, click 'Copy' to copy the schema or 'Download' to save it as a .schema.json file.
Is my JSON data secure when using this tool?
Yes, completely. This tool processes everything in your browser using client-side JavaScript. Your JSON data never leaves your computer — there are no server uploads, no API calls, and no data storage. This makes it safe to use with sensitive API responses, proprietary data schemas, and confidential configuration files.
Which JSON Schema draft version should I use?
Choose based on your ecosystem: Draft 2020-12 (default) is the latest stable standard and is used by OpenAPI 3.1. Draft-07 is widely supported by validation libraries and tools, and is a safe choice for maximum compatibility. Draft-04 is needed for legacy systems and OpenAPI 3.0 compatibility. If unsure, start with 2020-12 for new projects or Draft-07 for broad library support.
How does the tool determine required fields?
By default, the tool marks all non-null properties as required in the generated schema. A field with a null value is excluded from the required array since null suggests the field may be absent or optional. You can toggle this behavior off entirely if you prefer schemas without required constraints, which is useful for documentation-only schemas.
What string formats does the tool detect?
When format detection is enabled, the tool analyzes string values and automatically adds the 'format' keyword for recognized patterns: date-time (ISO 8601 timestamps like '2024-01-15T10:30:00Z'), date ('2024-01-15'), time ('10:30:00'), email addresses, URIs/URLs, UUIDs, IPv4 addresses, and IPv6 addresses. This enriches your schema beyond basic type information and enables format-aware validation.
What does the 'additional properties' option do?
The 'additionalProperties' keyword in JSON Schema controls whether an object can contain properties not listed in the schema. When set to 'Allow' (default), the generated schema permits extra fields — useful for flexible APIs. When set to 'Deny', it adds '"additionalProperties": false' to object schemas, creating strict contracts that reject any unexpected fields. Use 'Deny' for API request validation where you want to catch typos and unknown fields.
Code Examples
// JSON to JSON Schema generator
function jsonToSchema(json, options = {}) {
const { draft = '2020-12', includeRequired = true, detectFormats = true } = options;
const schemaUri = {
'draft-04': 'http://json-schema.org/draft-04/schema#',
'draft-07': 'http://json-schema.org/draft-07/schema#',
'2020-12': 'https://json-schema.org/draft/2020-12/schema',
}[draft];
function inferSchema(value) {
if (value === null) return { type: 'null' };
if (typeof value === 'boolean') return { type: 'boolean' };
if (typeof value === 'number') {
return { type: Number.isInteger(value) ? 'integer' : 'number' };
}
if (typeof value === 'string') {
const schema = { type: 'string' };
if (detectFormats) {
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(value)) schema.format = 'date-time';
else if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) schema.format = 'email';
else if (/^https?:\/\//.test(value)) schema.format = 'uri';
}
return schema;
}
if (Array.isArray(value)) {
if (value.length === 0) return { type: 'array', items: {} };
const itemSchemas = value.map(inferSchema);
const unique = dedup(itemSchemas);
return { type: 'array', items: unique.length === 1 ? unique[0] : { oneOf: unique } };
}
if (typeof value === 'object') {
const properties = {};
const required = [];
for (const [key, val] of Object.entries(value)) {
properties[key] = inferSchema(val);
if (includeRequired && val !== null) required.push(key);
}
const schema = { type: 'object', properties };
if (required.length > 0) schema.required = required;
return schema;
}
return {};
}
function dedup(schemas) {
const seen = new Map();
for (const s of schemas) {
const key = JSON.stringify(s);
if (!seen.has(key)) seen.set(key, s);
}
return [...seen.values()];
}
const parsed = JSON.parse(json);
return { $schema: schemaUri, ...inferSchema(parsed) };
}
// Usage
const json = '{"name":"Alice","age":30,"email":"alice@example.com"}';
console.log(JSON.stringify(jsonToSchema(json), null, 2));