JSON Schema Generator

By Cobra · Last updated

How inference works

Paste a JSON value. The generator walks objects and arrays, records property names, and emits a draft-07-ish schema with type, properties, and items. Nested objects become nested schemas. Numbers are typed as number, or integer when every sample value is integral.

Empty arrays become a generic array type because item shape cannot be inferred. Null values become a null type for that field. Mixed null and string in one sample still needs a human to decide on unions.

Worked example: inferring types from a sample object

Input {"id":1,"name":"Ada","tags":["seo","tools"],"active":true} sketches an object with integer id, string name, boolean active, and an array of strings for tags.

The draft will not mark required automatically. Add that array after you decide which fields must always appear in production payloads.

Limits

Inference from one sample underfits real data. Formats (email, uri), min/max, pattern, and enums need edits. Discriminated unions and oneOf are out of scope for this starter.

Use the JSON Schema validator next to test tightened schemas against more examples before you enforce them in CI.

Checklist before you enforce

After inference, walk every property: is the type too wide (number vs integer), should null be allowed, and which fields belong in required? Add additionalProperties: false only when you truly want unknown keys rejected. Keep a second sample that represents a failing payload so the validator proves both sides.

Worked example: adding rules a sample cannot show

From the sample {"id":7,"email":"[email protected]","tags":["x"]} a generator infers id as integer, email as string, and tags as an array of strings. It cannot know business rules: that email is required, that id must be positive, or that tags may be empty. Add required, minimum, and format by hand, then test with the JSON schema validator.

Pitfalls

Generate JSON Schema from data or types in code

JavaScript and Python infer a schema from sample objects, like this page. In Go, the usual route is to generate the schema from your struct type instead.

JavaScript: genson-js

import { createSchema } from "genson-js"; // npm i genson-js

const sample = { id: 7, name: "Ada", tags: ["admin"], active: true };
const schema = createSchema(sample);
console.log(JSON.stringify(schema, null, 2));
// Inferred types only: add required, formats and limits by hand afterwards

Python: GenSON

import json
from genson import SchemaBuilder  # pip install genson

builder = SchemaBuilder()
builder.add_object({"id": 7, "name": "Ada", "tags": ["admin"]})
builder.add_object({"id": 8, "name": "Grace", "email": "[email protected]"})  # merge more samples
print(json.dumps(builder.to_schema(), indent=2))  # only keys seen in every sample are required

Go: github.com/invopop/jsonschema (from structs)

package main

import (
	"encoding/json"
	"fmt"

	"github.com/invopop/jsonschema" // go get github.com/invopop/jsonschema
)

// In Go, the usual path is to generate the schema from your struct type.
type User struct {
	ID    int      `json:"id"`
	Name  string   `json:"name" jsonschema:"minLength=1"`
	Email string   `json:"email,omitempty" jsonschema:"format=email"`
	Tags  []string `json:"tags,omitempty"`
}

func main() {
	s := jsonschema.Reflect(&User{}) // draft 2020-12
	out, _ := json.MarshalIndent(s, "", "  ")
	fmt.Println(string(out))
}

Each snippet was run on October 14, 2026 with genson-js 0.0.8, GenSON 1.4.0 and invopop/jsonschema v0.14.0.

JSON Schema references

FAQ

Which draft does this target?

A practical draft-07 shaped schema for type, properties, and items. It is not a full formal verifier of every draft keyword.

Does it upload my JSON?

No. Inference runs locally in the browser.

Why is required missing?

A single example cannot tell optional from required. Mark required fields after you review the draft.

Empty array in the sample?

Item type cannot be inferred from []. The schema keeps a generic array. Paste a sample that includes at least one item when you care about item shape.

Which draft does the schema use?

Check the $schema line in the output and keep your validator on the same draft.

Can I generate from an array of objects?

Yes, but if objects differ, review the result: the item schema may only reflect fields from the sample it read.