JSON to JSON Schema
Convert JSON to Schema Online
Use our free json to schema generator to create accurate JSON Schema definitions from any JSON data instantly. Whether you are building json schema generator outputs for API validation, creating contract definitions for microservice communication, or documenting data structures for team collaboration, this tool produces comprehensive JSON Schema from your sample JSON input.
What is JSON Format
JSON, short for JavaScript Object Notation, is a lightweight data interchange format that serves as the universal standard for web-based data communication. Its syntax of key-value pairs enclosed in curly braces and ordered arrays enclosed in square brackets makes it intuitive for both humans and machines. JSON supports strings, numbers, booleans, null, nested objects, and arrays as value types.
JSON has become the default format for REST API responses, NoSQL database storage, configuration files, and data exchange between microservices. Every modern programming language includes built-in or standard library support for parsing and generating JSON. While JSON excels at representing data, it does not inherently define the rules or constraints that valid data must follow, which is where JSON Schema fills the gap by providing a vocabulary for describing and validating JSON document structure.
What is JSON Schema Format
JSON Schema is a declarative language that allows you to annotate and validate the structure, constraints, and documentation of JSON documents. Written in JSON itself, a JSON Schema defines the expected types, required properties, value ranges, string patterns, and structural rules that a valid JSON document must satisfy. The specification is maintained by the JSON Schema organization and has evolved through several drafts, with Draft 2020-12 being the most recent stable version.
A JSON Schema document uses keywords like type to specify data types, properties to define object fields, items to describe array elements, required to list mandatory properties, and additional keywords for constraints such as minimum, maximum, minLength, maxLength, pattern, and enum. Schemas support composition through allOf, anyOf, oneOf, and not keywords, as well as reusable definitions through the defs keyword and ref references. JSON Schema is widely used in OpenAPI specifications for REST API documentation, form validation in web applications, configuration file validation, and data contract enforcement between microservices.
How the Conversion Works
Converting JSON to JSON Schema involves analyzing the structure, types, and values of a sample JSON document to infer a schema that describes valid documents matching that structure. The converter parses the input JSON, examines each value to determine its type, and generates schema definitions with appropriate type constraints, required property lists, and nested schema definitions for complex structures. The resulting schema can then be used to validate other JSON documents against the inferred structure.
The conversion engine handles complex scenarios including nested objects that generate nested schema properties, arrays that produce items schemas describing element types, and mixed-type arrays that use anyOf composition. If you need to work with your JSON data in other formats, you can convert JSON to YAML for human-readable configuration files or transform JSON into XML for enterprise system integration. For generating typed code from your JSON, explore our JSON to TypeScript converter or JSON to Go struct generator.
Syntax Comparison
Understanding how JSON data maps to JSON Schema keywords clarifies the conversion process. A simple JSON object like this:
// JSON
{
"name": "Alice",
"age": 30,
"active": true
}
Generates the following JSON Schema:
// JSON Schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"active": { "type": "boolean" }
},
"required": ["name", "age", "active"]
}
Each JSON value type maps to a schema type keyword: strings become "type": "string", whole numbers become "type": "integer", decimal numbers become "type": "number", booleans become "type": "boolean", and null becomes "type": "null". Arrays generate an items keyword describing the element schema, and nested objects produce nested properties definitions. All properties found in the sample are listed in the required array by default since the converter cannot determine optionality from a single sample.
Common Use Cases
The most common use case for JSON to Schema conversion is creating validation schemas for REST API endpoints. API developers need to validate incoming request bodies against expected structures, and generating a schema from a sample payload provides an accurate starting point. These schemas can be integrated into OpenAPI specifications, used with validation libraries like Ajv in JavaScript or jsonschema in Python, or enforced at the API gateway level.
Another frequent scenario involves documenting data contracts between microservices. Teams that exchange JSON messages through message queues, event streams, or direct API calls use JSON Schema as a formal contract that both producers and consumers agree upon. Configuration file validation is another common use case, where applications validate their JSON configuration files against a schema at startup to catch errors early. Form generation tools also use JSON Schema to automatically create user interface forms that match the expected data structure.
JSON to Schema Examples
The following examples demonstrate how various JSON structures are converted into JSON Schema definitions using this json schema generator. These cover the most common patterns encountered when creating validation schemas from sample data.
A JSON object with nested structures generates a comprehensive schema:
// JSON Input
{
"product": {
"id": 42,
"name": "Wireless Mouse",
"price": 29.99,
"inStock": true,
"tags": ["electronics", "peripherals"],
"manufacturer": {
"name": "TechCorp",
"country": "Germany"
}
}
}
// JSON Schema Output
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"product": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"price": { "type": "number" },
"inStock": { "type": "boolean" },
"tags": {
"type": "array",
"items": { "type": "string" }
},
"manufacturer": {
"type": "object",
"properties": {
"name": { "type": "string" },
"country": { "type": "string" }
},
"required": ["name", "country"]
}
},
"required": ["id", "name", "price", "inStock", "tags", "manufacturer"]
}
},
"required": ["product"]
}
Frequently Asked Questions
Which JSON Schema draft version does the generator use?
The generator produces schemas conforming to JSON Schema Draft 2020-12, which is the most recent stable version of the specification. The generated schema includes the $schema keyword pointing to the Draft 2020-12 meta-schema URI. If your tooling requires an older draft version such as Draft-07 or Draft-04, you can update the $schema URI and adjust any incompatible keywords accordingly, though most keywords are backward compatible across recent drafts.
Are all properties marked as required by default?
Yes, since the converter works from a single JSON sample, it cannot determine which properties are optional and which are mandatory. Therefore, all properties found in the sample are included in the required array by default. After generating the schema, you should review the required list and remove any properties that are optional in your actual data model. This manual refinement step ensures the schema accurately reflects the flexibility of your real-world data.
How are JSON arrays represented in the schema?
JSON arrays generate a schema with "type": "array" and an items keyword describing the expected element type. For arrays containing elements of a single type, the items schema specifies that type directly. For arrays containing objects, the items schema includes a full object schema with properties and required lists. For arrays with mixed types, the items schema uses anyOf to list all observed element types as valid alternatives.
Can I use the generated schema with OpenAPI?
Yes, JSON Schema is the foundation of the schema object in OpenAPI specifications. The generated schema can be used directly in OpenAPI 3.1 documents, which fully support JSON Schema Draft 2020-12. For OpenAPI 3.0, minor adjustments may be needed since that version uses a subset of JSON Schema Draft-07. You can place the generated schema in the components/schemas section of your OpenAPI document and reference it from request body and response definitions.
How does the converter handle null values?
When the converter encounters a null value in the JSON input, it generates a type that allows null. In Draft 2020-12, this is represented as "type": ["string", "null"] or the appropriate type paired with null. Since the converter can only see null and not the intended non-null type, it may default to a generic nullable type. You should review null fields and specify the expected non-null type based on your knowledge of the data model.
Can I add custom validation constraints to the generated schema?
The generated schema provides the structural foundation, and you can enhance it with additional validation constraints after generation. Common additions include minimum and maximum for numeric ranges, minLength and maxLength for string lengths, pattern for regex validation, enum for allowed value lists, and format for semantic validation like email, uri, or date-time. These refinements transform the basic structural schema into a comprehensive validation tool.
Is the conversion performed securely?
Yes, the entire JSON to Schema conversion runs client-side in your browser. No data is transmitted to any external server, ensuring complete privacy and security. This makes the tool safe for generating schemas from JSON containing sensitive information such as API responses with user data, financial records, or proprietary business structures. Your input never leaves your browser session.
How does JSON Schema compare to TypeScript interfaces?
JSON Schema and TypeScript interfaces both describe data shapes, but they serve different purposes. JSON Schema is a runtime validation tool that can validate actual JSON data against defined rules, while TypeScript interfaces are compile-time constructs that are erased during compilation. JSON Schema supports constraints like minimum values and string patterns that TypeScript cannot express. For compile-time type safety, use our JSON to TypeScript converter. For runtime validation, JSON Schema is the appropriate choice.
What is the difference between type integer and type number?
In JSON Schema, "type": "integer" matches only whole numbers without a decimal point, while "type": "number" matches any numeric value including decimals. The converter uses integer for JSON values like 42 and number for values like 42.5. If a field should accept both integers and decimals, you can change the type to number, which is the more permissive option. This distinction allows precise validation of numeric fields in your data.
FAQ
How does JSON to JSON Schema work?
Generate JSON Schema from JSON data.