2026-04-11 · 5 min read · json-schema · validation · zod · typescript

Auto-Inferring JSON Schemas from Samples — and Why You Should

The pain\n\nHand-writing JSON schemas is soul-crushing. Especially for API responses with 30+ fields and nested objects. And when the upstream API changes a field from string to string | null, your schema silently drifts until production bites.\n\n## Auto-inference changes the math\n\nGiven 5-10 real samples of a JSON object, an inference engine can produce a schema that:\n\n- Captures every field with its observed type union\n- Marks sometimes-present fields as optional\n- Detects formats (email, date-time, URI) by regex matching\n- Nests correctly for arrays and sub-objects\n- Generates TypeScript types + zod validators as a byproduct\n\nThe accuracy vs a hand-written schema is typically 85-95% — and the 5-15% gap is usually things the human would have gotten wrong too.\n\n## The workflow\n\n1. Collect 5-10 real samples from production\n2. Run inference → candidate schema\n3. Eyeball the schema for obvious misses\n4. Commit to source control\n5. When upstream changes → re-infer from new samples → diff → fix drift\n\n## The free API\n\nJSON Schema Validator — auto-infer from samples, validate payloads, generate TypeScript, generate mock data, diff two schemas, produce docs. Free 100/day. Pro $14.99/mo unlimited.\n\n``bash\ncurl -X POST https://api.lazy-mac.com/json-schema/api/v1/infer \\\n -d '{\"samples\":[{\"name\":\"Alice\",\"age\":30},{\"name\":\"Bob\",\"age\":25,\"email\":\"bob@ex.com\"}]}'\n`\n\nReturns:\n\n`json\n{\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\"type\": \"string\"},\n \"age\": {\"type\": \"integer\"},\n \"email\": {\"type\": \"string\", \"format\": \"email\"}\n },\n \"required\": [\"name\", \"age\"]\n}\n`\n\n## Generate zod from the schema\n\n`bash\ncurl -X POST https://api.lazy-mac.com/json-schema/api/v1/to-zod -d @schema.json\n`\n\nReturns a ready-to-paste zod schema file. No more manual sync between your types and runtime validation.\n\n## Why this matters for MCP\n\nMCP tool authors need an inputSchema` for every tool. Hand-writing it is tedious, and incomplete schemas are one of the 10 MCPWatch security checks. Auto-infer from real tool call samples → get a complete schema → drop into your MCP tool registration. Free tier covers most MCP dev workflows.


📬 MCP Security Weekly

One email per week — new CVEs, scanner improvements, MCPWatch grade drops on popular servers. Free. Unsubscribe anytime.

Support the work: MCP Pro $29/mo · MCPWatch Pro Report $49 · more posts