All Recipes
StarVerified Developer Solution • 100% Offline
JSON to Zod Schema

Fix "ZodError: Expected string, received number" in API validation

Solve runtime Zod schema validation errors when parsing API payloads that contain unexpected types.

The Problem (Error Root Cause)Exception

Your application crashes at runtime when calling `schema.parse()` because an API returned a number (like `123`) instead of a string (like `"123"`), breaking your strict Zod schema.

Identified via runtime validation & stack traces
The Solution (Step-by-Step Fix)Verified

If the API is inconsistent, update your Zod schema to coerce the type using `z.coerce.string()` or strictly match the API's actual JSON structure by regenerating your schema directly from the API response payload.

Deterministic, non-destructive resolution

Code Standard: Bad Pattern vs Verified Fix

Live Syntax
Anti-Pattern vs Verified Fix
1// ❌ Bad: Strict schema crashes on number
2const schema = z.object({ id: z.string() });
3schema.parse({ id: 123 }); // Throws ZodError
4
5// ✅ Good: Coerce types or use accurate schemas
6const schema = z.object({ id: z.coerce.string() });
7schema.parse({ id: 123 }); // Returns { id: "123" }

Test and resolve this using JSON to Zod Schema

Execute directly in your browser memory. Zero API keys, zero network tracking, completely client-side.

Launch JSON to Zod Schema

Frequently Asked Questions

Q:Why use Zod over TypeScript interfaces?

TypeScript interfaces are stripped out at compile time. Zod validates data at runtime, preventing bad data from entering your app.

Q:How do I create a Zod schema from a large JSON?

Use our JSON to Zod Schema tool to instantly generate complete, nested validation schemas from any JSON payload.