npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@adesso-se/zod-from-json-samples

v1.0.0

Published

Generate a single, robust Zod schema from multiple JSON files, with support for optional fields, union types, and OpenAPI examples.

Readme

@adesso-se/zod-from-json-samples

This utility generates a single, robust Zod schema from one or more JSON files. It intelligently infers types by merging the structures of multiple JSON samples, with built-in support for optional fields, union types, and adding examples for OpenAPI documentation.

Usage

Install the script using and run it by providing a glob pattern to select your JSON sample files. Remember to enclose the pattern in quotes.

npm install @adesso-se/zod-from-json-samples -g

samples2zod "path/to/your/json_samples/**/*.json" -f

The script will process all matching files and generate a schema.js file in your current working directory, containing the final Zod schema.

Options

  • --force-optional, -f Force all inferred properties to be optional. This is useful if your JSON schema is unstable or if you want to validate objects that may only contain a subset of the properties found in your samples.

Example

Given two JSON files:

user1.json:

{
  "id": 1,
  "name": "John Doe",
  "email": "[email protected]"
}

user2.json:

{
  "id": 2,
  "name": "Jane Doe",
  "email": null,
  "age": 30
}

Running the standard command:

npx @adesso-se/zod-from-json-samples "user*.json"

Will produce schema.js with the following content, where age is automatically detected as optional:

export const MySchema = z.object({
  id: z.number().openapi({ examples: [1, 2] }),
  name: z.string().openapi({ examples: ["John Doe", "Jane Doe"] }),
  email: z
    .string()
    .openapi({ examples: ["[email protected]"] })
    .nullable(),
  age: z
    .number()
    .openapi({ examples: [30] })
    .optional(),
});

Example with --force-optional

Running the command with the new flag:

npx @adesso-se/zod-from-json-samples "user*.json" --force-optional

Will produce schema.js where all properties are now optional:

export const MySchema = z.object({
  id: z
    .number()
    .openapi({ examples: [1, 2] })
    .optional(),
  name: z
    .string()
    .openapi({ examples: ["John Doe", "Jane Doe"] })
    .optional(),
  email: z
    .string()
    .openapi({ examples: ["[email protected]"] })
    .nullable()
    .optional(),
  age: z
    .number()
    .openapi({ examples: [30] })
    .optional(),
});

Features

  • Type Inference: Analyzes JSON files to infer data types (string, number, boolean, array, object, null).
  • Schema Merging: Combines multiple JSON structures into a single, comprehensive schema.
  • Optional Fields: Automatically marks fields as optional if they don't appear in every JSON sample.
  • Union Types: Creates union types when a field has different data types across samples.
  • OpenAPI Examples: Includes values from your JSON files as examples in the generated schema using .openapi({ examples: [...] }).

Zod Tip: Making an Existing Schema Optional

If you have already generated a schema and want to make all its properties optional without re-running the tool, Zod provides a handy .partial() method.

This is especially useful for update operations (e.g., PATCH requests) where only a subset of fields may be present.

import { z } from "zod";

// Assuming MySchema is your generated schema
import { MySchema } from "./schema.js";

// Create a new schema with all properties being optional
const PartialSchema = MySchema.partial();

// Now you can validate an object with only some of the properties
const result = PartialSchema.safeParse({ name: "Jane Doe" }); // ✅ Success!