JSON Schema Generator
Infer a JSON Schema (Draft 2020-12) from any JSON object. Detects types, nested objects, arrays, required fields, and common string formats.
What is JSON Schema?
JSON Schema is a vocabulary for describing and validating JSON data. A schema defines the shape of a JSON document — which fields exist, what types they are, which are required, and what formats strings must follow. The generated schema can be used directly with JSONKit's JSON Schema Validator.
| JSON type | Schema type | Notes |
|---|---|---|
| string | "type": "string" | Optional format: email, uuid, date-time, date, uri |
| number (integer) | "type": "integer" | Only when the number has no decimal part |
| number (float) | "type": "number" | Allows any numeric value including decimals |
| true / false | "type": "boolean" | |
| null | "type": "null" | Field is optional when null in the sample |
| object | "type": "object" | Generates properties, required, additionalProperties: false |
| array | "type": "array" | items inferred from the first element |
Where This Saves the Most Time
- ▸Bootstrapping a contract from a real payload — Capture one real API response and generate a starting schema instead of hand-writing every property from scratch.
- ▸Documenting an undocumented API — Turn a sample response from an internal or third-party API into a schema you can attach to internal docs or an OpenAPI spec.
- ▸Feeding a validator or CI check — Generate here, then paste the result straight into the JSON Schema Validator (or Ajv/jsonschema in your codebase) to start enforcing the shape.
- ▸Kick-starting type generation — A JSON Schema is a common input for codegen tools (quicktype, json-schema-to-typescript) that produce typed interfaces across languages.
- ▸Spotting accidental nulls or inconsistent types — Generating a schema from a sample forces you to notice which fields were null or unexpectedly typed in that specific response.
Using the Generated Schema in Node.js (Ajv)
javascript
// npm install ajv ajv-formats
import Ajv from "ajv";
import addFormats from "ajv-formats";
const ajv = new Ajv({ allErrors: true });
addFormats(ajv); // required for "format": "email" / "date-time" / "uuid" to be enforced
const schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "age", "email"],
"additionalProperties": false
};
const validate = ajv.compile(schema);
const document = { name: "Ravi", age: 28, email: "ravi@example.com" };
if (validate(document)) {
console.log("Document is valid");
} else {
console.log(validate.errors);
}Using the Generated Schema in Go
go
// go get github.com/xeipuuv/gojsonschema
import "github.com/xeipuuv/gojsonschema"
schemaLoader := gojsonschema.NewStringLoader(`{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "age", "email"],
"additionalProperties": false
}`)
documentLoader := gojsonschema.NewStringLoader(`{"name":"Ravi","age":28,"email":"ravi@example.com"}`)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil { panic(err) }
if result.Valid() {
fmt.Println("Document is valid")
} else {
for _, desc := range result.Errors() {
fmt.Println("-", desc)
}
}Using the Schema in Python (Pydantic v2)
python
# pip install pydantic
from pydantic import BaseModel, EmailStr
from typing import Optional
# Or use jsonschema library directly:
# pip install jsonschema
import jsonschema, json
schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"email": {"type": "string", "format": "email"}
},
"required": ["name", "age", "email"],
"additionalProperties": False
}
document = {"name": "Ravi", "age": 28, "email": "ravi@example.com"}
jsonschema.validate(instance=document, schema=schema)
print("Valid!")