Convert a JSON API Response to a Zod Schema
Table of Contents
To convert a JSON API response to a Zod schema, paste the response JSON into the free converter, copy the generated schema, and paste it into your TypeScript project. The tool reads every field and outputs the correct Zod type — string, number, boolean, object, array, null, or union.
This page walks through the process end-to-end with real API examples.
Step 1: Get the Raw JSON Response
You need a real JSON sample from the API. Get it from:
- Postman or Insomnia: Run the request and copy the response body
- Browser DevTools: Open Network tab, find the API call, click Preview or Response
- curl:
curl https://api.example.com/users/1 - Your code: Add a temporary
console.log(JSON.stringify(response, null, 2))
If the JSON has syntax errors (copy issues, incomplete responses), fix them first with the free JSON formatter — it highlights exactly where the error is.
Step 2: Generate the Zod Schema
Open the JSON to Zod converter and paste your JSON. The schema appears instantly.
Example input — a GitHub-style user response:
{
"id": 42,
"login": "alice",
"name": "Alice Smith",
"email": "[email protected]",
"public_repos": 14,
"followers": 203,
"created_at": "2020-03-15T10:22:00Z",
"plan": {
"name": "pro",
"space": 976562499,
"private_repos": 9999
}
}
Generated output:
const schema = z.object({
id: z.number(),
login: z.string(),
name: z.string(),
email: z.string(),
public_repos: z.number(),
followers: z.number(),
created_at: z.string(),
plan: z.object({
name: z.string(),
space: z.number(),
private_repos: z.number()
})
});
Sell Custom Apparel — We Handle Printing & Free Shipping
Step 3: Refine the Schema
The generated schema captures the structure. Now add domain-specific constraints:
const UserSchema = z.object({
id: z.number().int().positive(),
login: z.string().min(1).max(39),
name: z.string(),
email: z.string().email().nullable(),
public_repos: z.number().int().min(0),
followers: z.number().int().min(0),
created_at: z.string().datetime(),
plan: z.object({
name: z.enum(['free', 'pro', 'team', 'enterprise']),
space: z.number().int().positive(),
private_repos: z.number().int().min(0)
}).optional()
});
type User = z.infer<typeof UserSchema>;
Step 4: Use in Your API Layer
Wrap your fetch call with the schema:
async function getUser(login: string) {
const res = await fetch("https://api.github.com/users/" + login);
const json = await res.json();
const result = UserSchema.safeParse(json);
if (!result.success) {
throw new Error("API response validation failed: " + result.error.message);
}
return result.data; // typed as User
}
If the API ever changes its response shape, the schema catches it immediately rather than letting corrupt data propagate silently through your application.
Try It Free — No Signup Required
Runs 100% in your browser. No data is collected, stored, or sent anywhere.
Open Free JSON to Zod ConverterFrequently Asked Questions
How do I convert a JSON API response to a Zod schema?
Paste the JSON response into the free converter at /developer-tools/json-to-zod/. It generates the full Zod schema immediately. Copy it, add any custom constraints (.email(), .optional(), etc.), and use it in your TypeScript code.
What if the API response has null values?
Null values are mapped to z.null(). If the field can be a value OR null, change it to z.string().nullable() or z.union([z.string(), z.null()]) depending on the field.
What if I do not have a real API response to sample?
Check the API documentation for a sample response, use Postman or curl to make a live request, or look at existing test fixtures in your codebase that contain sample data.
Can the converter handle deeply nested JSON?
Yes. Nested objects become nested z.object() calls. The recursion goes as deep as your JSON structure. Arrays of objects become z.array(z.object({})).

