Online JSON to JSON Schema Generator
Generate robust, standards-compliant JSON Schema from real-world JSON—instantly, securely, and for free.
This tool takes a JSON sample and produces a JSON Schema Draft-07 document that accurately reflects its structure, data types, and constraints. All processing happens in your browser for complete privacy. No data is uploaded or stored anywhere. Ideal for API documentation, validation, and code generation.
Convert JSON to JSON Schema below
How Schema Generation Works Under the Hood
Generating JSON Schema from sample JSON involves more than mapping fields to types. This tool employs several key steps for correctness and completeness:
- Parsing: The tool parses your JSON according to ECMA-404, reporting detailed errors for malformed input or ambiguous constructs.
- Structural Analysis: It builds a tree representation of your data, classifying each property and element according to JSON Schema's core types (object, array, string, number, integer, boolean, null).
- Recursive Traversal: Deep and nested structures—including arrays of objects or nested arrays—are traversed recursively to generate nested schema definitions.
- Type Inference and Merging: For each property or array element, types are inferred. Where properties or items have mixed types, 'type' is expressed as an array (union type) per JSON Schema spec.
- Required vs. Optional Detection: By analyzing all objects at each level, the tool distinguishes between always-present (required) and sometimes-missing (optional) keys.
- Value Constraints: Where possible, the tool infers enums (set of allowed values), min/max for numbers, minLength/maxLength for strings, and format hints (such as 'email', 'uri', or 'date-time').
- Edge Case Handling: Special attention is given to empty arrays, nulls, and sparse structures—ensuring valid schemas that match real-world variability.
- Schema Synthesis: The final schema is emitted as a standards-compliant Draft-07 document—ready for use with tools like Ajv, OpenAPI, or code generation libraries.
Why Generate JSON Schema? Practical Uses
- Automated Validation: Use generated schemas to enforce data contracts and validate incoming payloads in your APIs, microservices, or CLIs.
- API Documentation: Auto-generate docs and interactive explorers (Swagger, Postman) with precise data definitions.
- Code Generation: Create type-safe models or validators in TypeScript, Python, Java, and other languages using your schema as the single source of truth.
- Test Data Generation: Tools like JSON Schema Faker or Mockaroo use schemas to synthesize realistic mock data for QA and load testing.
- Refactoring & Migration: Validate legacy or changing data structures, ensuring compatibility as your backend evolves.
Technical Features
- Draft-07 compatibility for maximum interoperability with major validators and API design platforms.
- Recursive analysis of any nesting depth—arrays within arrays, objects within arrays, and more.
- Accurate union type ('type' array) inference for fields or items that vary across samples.
- Automatic detection of enums, length, pattern, format, min/max, and other common constraints.
- Explicit support for nulls, empty arrays/objects, and partial records.
- Schema generation runs entirely in-browser. Your input JSON never leaves your device.
Technical Example: From Sample JSON to Draft-07 Schema
Sample JSON Input
{ "transaction": { "id": "txn_abc123", "amount": 99.95, "currency": "USD", "status": "completed", "meta": { "ip": "203.0.113.1", "tags": ["recurring", null] } }, "refunded": false, "notes": null }Generated JSON Schema Output
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "transaction": { "type": "object", "properties": { "id": { "type": "string" }, "amount": { "type": "number" }, "currency": { "type": "string", "minLength": 3, "maxLength": 3 }, "status": { "type": "string", "enum": ["completed"] }, "meta": { "type": "object", "properties": { "ip": { "type": "string", "format": "ipv4" }, "tags": { "type": "array", "items": { "type": ["string", "null"] } } }, "required": ["ip", "tags"] } }, "required": ["id", "amount", "currency", "status", "meta"] }, "refunded": { "type": "boolean" }, "notes": { "type": ["null"] } }, "required": ["transaction", "refunded", "notes"] }
How to Use This JSON to JSON Schema Tool
- Paste or type your example JSON into the editor below. The tool accepts any valid JSON, from simple objects to deeply nested structures.
- Click 'Generate JSON Schema' to generate and view the produced JSON Schema.
- Copy, edit, or download the schema. Integrate it directly into your API definitions, validation logic, or documentation.
Code Examples for JSON to Schema Generation
See how to generate JSON Schema from JSON in various programming languages.
JavaScript (Node.js)
Install: npm install jsonschema-generator
const generateSchema = require('jsonschema-generator');
const data = {
name: "Alice",
age: 30,
isActive: true,
tags: ["user", "admin"]
};
const schema = generateSchema(data);
console.log(JSON.stringify(schema, null, 2));
JavaScript (Node.js) with generate-schema
Install: npm install generate-schema
const GenerateSchema = require('generate-schema');
const data = { name: "Alice", age: 30, tags: ["admin", "user"] };
const schema = GenerateSchema.json('User', data);
console.log(JSON.stringify(schema, null, 2));
Python with genson
Install: pip install genson
from genson import SchemaBuilder
sample = {"name": "Alice", "age": 30, "tags": ["user", "admin"]}
builder = SchemaBuilder()
builder.add_object(sample)
print(builder.to_json(indent=2))
Python with jsonschema-generate
Install: pip install jsonschema-generate
from jsonschema_generate import generate_schema
sample = {"name": "Alice", "age": 30, "tags": ["user", "admin"]}
schema = generate_schema(sample)
print(schema)
Go
Install: go get github.com/invopop/jsonschema
package main
import (
"encoding/json"
"fmt"
"github.com/invopop/jsonschema"
)
type User struct {
Name string `json:"name"
Age int `json:"age"
Tags []string `json:"tags"
}
func main() {
schema := jsonschema.Reflect(&User{})
out, _ := json.MarshalIndent(schema, "", " ")
fmt.Println(string(out))
}
Java with jsonschema2pojo (CLI/Gradle/Maven)
Install: jsonschema2pojo CLI or plugin (see https://www.jsonschema2pojo.org/)
# Generate Java POJOs *from* a JSON Schema, not the reverse.
# For schema generation from Java, see tools like jackson-module-jsonSchema.
# See: https://github.com/FasterXML/jackson-module-jsonSchema
C# with NJsonSchema
Install: dotnet add package NJsonSchema
using NJsonSchema;
using Newtonsoft.Json.Linq;
var sample = JObject.Parse("{\"name\":\"Alice\",\"age\":30, \"tags\":[\"user\"]}");
var schema = await JsonSchema.FromSampleJsonAsync(sample.ToString());
Console.WriteLine(schema.ToJson());
PHP with swaggest/json-schema
Install: composer require swaggest/json-schema
require 'vendor/autoload.php';
use Swaggest\JsonSchema\Structure\ClassStructure;
$sample = ["name" => "Alice", "age" => 30, "tags" => ["user"]];
$schema = ClassStructure::exportSchema($sample);
echo json_encode($schema, JSON_PRETTY_PRINT);
Ruby with json_schemer
Install: gem install json_schemer
require 'json_schemer'
sample = { "name" => "Alice", "age" => 30, "tags" => ["admin", "user"] }
schema = JSONSchemer.schema(sample)
puts schema.to_json
Bash (with Python genson)
Install: pip install genson
echo '{"name":"Alice","age":30,"tags":["user","admin"]}' | genson | jq .
Rust with schemars
Install: cargo add schemars
use schemars::JsonSchema;
use serde::Serialize;
#[derive(Serialize, JsonSchema)]
struct User {
name: String,
age: u32,
tags: Vec<String>,
}
fn main() {
let schema = schemars::schema_for!(User);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
Scala with com.github.andyglow:jsonschema
Install: libraryDependencies += "com.github.andyglow" %% "jsonschema" % "0.7.7"
import json.schema._
case class User(name: String, age: Int, tags: List[String])
val schema = Json.schema[User]
println(schema.asSpray.prettyPrint)
TypeScript with typescript-json-schema
Install: npm install -g typescript-json-schema
# Generate schema from a TypeScript interface:
typescript-json-schema tsconfig.json User --out schema.json
# See https://github.com/YousefED/typescript-json-schema
Dart (manual, no auto-inference)
Install: None (write schema manually)
// Dart does not have an automatic JSON Schema generator yet.
// Manually define schema as a Dart Map or use online tools.