Gathering detailed insights and metrics for json-schema-to-zod
Gathering detailed insights and metrics for json-schema-to-zod
Gathering detailed insights and metrics for json-schema-to-zod
Gathering detailed insights and metrics for json-schema-to-zod
npm install json-schema-to-zod
Typescript
Module System
Node Version
NPM Version
99.3
Supply Chain
99.6
Quality
83.9
Maintenance
100
Vulnerability
100
License
TypeScript (96.31%)
JavaScript (3.69%)
Total Downloads
5,179,777
Last Day
21,629
Last Week
100,070
Last Month
462,119
Last Year
3,376,372
ISC License
438 Stars
235 Commits
56 Forks
3 Watchers
1 Branches
19 Contributors
Updated on May 07, 2025
Minified
Minified + Gzipped
Latest Version
2.6.1
Package Id
json-schema-to-zod@2.6.1
Unpacked Size
106.41 kB
Size
16.63 kB
File Count
89
NPM Version
10.9.2
Node Version
22.14.0
Published on
Apr 04, 2025
Cumulative downloads
Total Downloads
7
Looking for the exact opposite? Check out zod-to-json-schema
A runtime package and CLI tool to convert JSON schema (draft 4+) objects or files into Zod schemas in the form of JavaScript code.
Before v2 it used prettier
for formatting and json-refs
to resolve schemas. To replicate the previous behaviour, please use their respective CLI tools.
Since v2 the CLI supports piped JSON.
Just paste your JSON schemas here!
1npm i -g json-schema-to-zod
1json-schema-to-zod -i mySchema.json -o mySchema.ts
$refs
resolved and output formatted1npm i -g json-schema-to-zod json-refs prettier
1json-refs resolve mySchema.json | json-schema-to-zod | prettier --parser typescript > mySchema.ts
Flag | Shorthand | Function |
---|---|---|
--input | -i | JSON or a source file path. Required if no data is piped. |
--output | -o | A file path to write to. If not supplied stdout will be used. |
--name | -n | The name of the schema in the output |
--depth | -d | Maximum depth of recursion in schema before falling back to z.any() . Defaults to 0. |
--module | -m | Module syntax; esm , cjs or none. Defaults to esm in the CLI and none programmaticly. |
--type | -t | Export a named type along with the schema. Requires name to be set and module to be esm . |
--noImport | -ni | Removes the import { z } from 'zod'; or equivalent from the output. |
--withJsdocs | -wj | Generate jsdocs off of the description property. |
1import { jsonSchemaToZod } from "json-schema-to-zod"; 2 3const myObject = { 4 type: "object", 5 properties: { 6 hello: { 7 type: "string", 8 }, 9 }, 10}; 11 12const module = jsonSchemaToZod(myObject, { module: "esm" }); 13 14// `type` can be either a string or - outside of the CLI - a boolean. If its `true`, the name of the type will be the name of the schema with a capitalized first letter. 15const moduleWithType = jsonSchemaToZod(myObject, { 16 name: "mySchema", 17 module: "esm", 18 type: true, 19}); 20 21const cjs = jsonSchemaToZod(myObject, { module: "cjs", name: "mySchema" }); 22 23const justTheSchema = jsonSchemaToZod(myObject);
module
1import { z } from "zod"; 2 3export default z.object({ hello: z.string().optional() });
moduleWithType
1import { z } from "zod"; 2 3export const mySchema = z.object({ hello: z.string().optional() }); 4export type MySchema = z.infer<typeof mySchema>;
cjs
1const { z } = require("zod"); 2 3module.exports = { mySchema: z.object({ hello: z.string().optional() }) };
justTheSchema
1z.object({ hello: z.string().optional() });
$refs
resolved and output formatted1import { z } from "zod"; 2import { resolveRefs } from "json-refs"; 3import { format } from "prettier"; 4import jsonSchemaToZod from "json-schema-to-zod"; 5 6async function example(jsonSchema: Record<string, unknown>): Promise<string> { 7 const { resolved } = await resolveRefs(jsonSchema); 8 const code = jsonSchemaToZod(resolved); 9 const formatted = await format(code, { parser: "typescript" }); 10 11 return formatted; 12}
You can pass a function to the overrideParser
option, which represents a function that receives the current schema node and the reference object, and should return a string when it wants to replace a default output. If the default output should be used for the node just return void.
Factored schemas (like object schemas with "oneOf" etc.) is only partially supported. Here be dragons.
The output of this package is not meant to be used at runtime. JSON Schema and Zod does not overlap 100% and the scope of the parsers are purposefully limited in order to help the author avoid a permanent state of chaotic insanity. As this may cause some details of the original schema to be lost in translation, it is instead recommended to use tools such as Ajv to validate your runtime values directly against the original JSON Schema.
That said, it's possible in most cases to use eval
. Here's an example that you shouldn't use:
1const zodSchema = eval(jsonSchemaToZod({ type: "string" }, { module: "cjs" })); 2 3zodSchema.safeParse("Please just use Ajv instead");
No vulnerabilities found.
No security vulnerabilities found.
Last Day
-5.2%
21,629
Compared to previous day
Last Week
-11.9%
100,070
Compared to previous week
Last Month
28%
462,119
Compared to previous month
Last Year
108.7%
3,376,372
Compared to previous year