{"id":10434,"library":"zod-to-json-schema","title":"Zod to JSON Schema Converter","description":"Zod to Json Schema is a utility library (current stable version 3.25.2) designed to convert Zod schemas into JSON schemas. It supports various schema types, validations, and resolves recursive schemas using `$ref`s. However, this project is officially deprecated as of November 2025, recommending users transition to Zod v4, which offers native JSON schema generation.","status":"deprecated","version":"3.25.2","language":"javascript","source_language":"en","source_url":"https://github.com/StefanTerdell/zod-to-json-schema","tags":["javascript","zod","json","schema","open","api","conversion","typescript"],"install":[{"cmd":"npm install zod-to-json-schema","lang":"bash","label":"npm"},{"cmd":"yarn add zod-to-json-schema","lang":"bash","label":"yarn"},{"cmd":"pnpm add zod-to-json-schema","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For Zod v3.25 and v4, you can also use `import { z } from 'zod/v3';` if you need explicit v3 types.","symbol":"z","correct":"import { z } from 'zod';"},{"symbol":"zodToJsonSchema","correct":"import { zodToJsonSchema } from 'zod-to-json-schema';"}],"quickstart":{"code":"import { z } from 'zod';\nimport { zodToJsonSchema } from 'zod-to-json-schema';\n\nconst mySchema = z\n  .object({\n    myString: z.string().min(5),\n    myUnion: z.union([z.number(), z.boolean()]),\n  })\n  .describe('My neat object schema');\n\nconst jsonSchema = zodToJsonSchema(mySchema, 'mySchema');\n\nconsole.log(JSON.stringify(jsonSchema, null, 2));\n/*\nExpected output:\n{\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"$ref\": \"#/definitions/mySchema\",\n  \"definitions\": {\n    \"mySchema\": {\n      \"description\": \"My neat object schema\",\n      \"type\": \"object\",\n      \"properties\": {\n        \"myString\": {\n          \"type\": \"string\",\n          \"minLength\": 5\n        },\n        \"myUnion\": {\n          \"type\": [\n            \"number\",\n            \"boolean\"\n          ]\n        }\n      },\n      \"additionalProperties\": false,\n      \"required\": [\n        \"myString\",\n        \"myUnion\"\n      ]\n    }\n  }\n}\n*/","lang":"typescript","description":"Converts a Zod object schema into a JSON schema, demonstrating basic type conversion, property validation, and the use of a schema name for `$ref` generation."},"warnings":[{"fix":"Migrate your schema generation to Zod v4, which includes native JSON schema generation via `z.tojsonSchema()`.","message":"This package is officially deprecated as of November 2025 and will no longer be actively maintained.","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"If your project uses Zod v4 and you need to convert v4-style schemas, use Zod's built-in `z.tojsonSchema()` method directly. If you must use `zod-to-json-schema` with Zod v4 installed, ensure your Zod schemas are written in a v3-compatible manner.","message":"Although `zod-to-json-schema` v3.25+ can use Zod v4 as a peer-dependency, it only supports converting Zod schemas defined using v3-compatible syntax. It does not fully support Zod v4-specific schema features.","severity":"gotcha","affected_versions":">=3.25.0"},{"fix":"Be aware of the target OpenAPI version. If you require OpenAPI 3.1 compliance, carefully review the generated schema and apply any necessary post-processing or configuration adjustments.","message":"The library primarily targets the OpenAPI 3.0 specification for JSON schema generation. OpenAPI 3.1 supports standard JSON Schema, so direct output might not be fully compliant without adjustments.","severity":"gotcha","affected_versions":"*"},{"fix":"Understand this specific conversion behavior. If it does not match your desired JSON schema for optional properties, review the options passed to `zodToJsonSchema` or post-process the output.","message":"When 'Open AI strict mode' is enabled via options, optional object properties in Zod schemas are converted to `required` but `nullable` properties in the generated JSON schema.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-18T00:00:00.000Z","next_check":"2026-07-17T00:00:00.000Z","problems":[{"fix":"Ensure Zod schemas are defined using v3-compatible syntax, or migrate to Zod v4's native `z.tojsonSchema()` for Zod v4 schemas.","cause":"Attempting to convert a Zod v4 schema that uses features not compatible with `zod-to-json-schema`'s v3 interpretation.","error":"Unsupported Zod schema type or structure for conversion."},{"fix":"Carefully review the `zod-to-json-schema` documentation for specific type support. Manually adjust the generated JSON schema if a Zod type's conversion is not as expected.","cause":"`zod-to-json-schema` might interpret certain complex Zod types differently than anticipated, or may not fully support all intricate Zod features.","error":"Generated JSON schema differs from expected output for a complex Zod type (e.g., union, intersection)."},{"fix":"Ensure the top-level schema is provided with a `name` via the second argument to `zodToJsonSchema` (e.g., `zodToJsonSchema(mySchema, 'MySchema')`) or through the options object to enable proper `$ref` generation.","cause":"Recursive or recurring schemas require a named definition to generate correct `$ref`s, but the schema was not provided with a name.","error":"JSON Schema validation error: Reference '#/definitions/...' could not be resolved."},{"fix":"If this conversion is not desired, review the options object passed to `zodToJsonSchema` and adjust or disable any strict mode or OpenAPI compatibility settings.","cause":"The 'Open AI strict mode' (or a similar configuration option) is enabled, which explicitly converts optional Zod properties to `required` and `nullable` in the resulting JSON schema.","error":"Optional properties in Zod schema are converted to `required` and `nullable` in the JSON schema output."}],"ecosystem":"npm"}