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 |
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!")