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

data-to-jsonschema

v1.0.1

Published

Convert plain javascript objects to JSON Schema

Readme

data-to-jsonschema

Convert plain javascript objects to JSON Schema.

Requirements: Node.js 20 or newer. This package is ESM-only — import only, no require.

Installation

npm install data-to-jsonschema

Example

Convert data to JSON Schema

import {
  createJsonSchemaValidator,
  generateJsonSchemaFromData,
} from "data-to-jsonschema";

const TEST_DATA = [
  {
    id: 1,
    name: "John Doe",
    age: 30,
    email: "[email protected]",
  },
  {
    id: 1,
    name: "John Doe",
    age: 30,
    email: "[email protected]",
    address: {
      street: "123 Main St",
      city: "Anytown",
      state: "NY",
      zip: "12345",
    },
  },
  {
    id: 1,
    name: "John Doe",
    age: 30,
    email: null,
    preferences: [{ name: "pref1", value: "value1" }],
  },
];

const jsonSchema = generateJsonSchemaFromData(TEST_DATA, "TestData", {
  additionalProperties: false,
});

console.log(JSON.stringify(jsonSchema, null, 2));

Create a validator function from the generated schema

const validator = createJsonSchemaValidator(jsonSchema)

console.log(validator({id: 1, name: "John Doe", age: 30, email: "[email protected]"}));
// { valid: true, errors: undefined }

console.log(validator({id: 1, name: "John Doe", age: 30 }));
// { valid: false, errors: [{...}] } — missing required `email`

Subpath imports

You can import individual functions from per-feature subpaths if you only need one piece of the library:

import { generateJsonSchemaFromData } from "data-to-jsonschema/generate-schema";
import { createJsonSchemaValidator } from "data-to-jsonschema/validator";
import { traverseObject } from "data-to-jsonschema/traverse";

import type { JsonSchema, JsonType } from "data-to-jsonschema/generate-schema";

The bundled barrel (import { ... } from "data-to-jsonschema") continues to work and re-exports everything.

Utilities

generateJsonSchemaFromData(data, title?, opts?)

Infer a JSON Schema (Draft-07) from an array of sample objects. Differing types for the same key become oneOf, array item types are combined via anyOf, and required is the intersection of keys across samples — a property is only required if it appears in every sample.

import { generateJsonSchemaFromData } from "data-to-jsonschema";

generateJsonSchemaFromData(
  [{ id: 1, name: "John" }, { id: 2, name: "Jane", age: 30 }],
  "User",
  { additionalProperties: false },
);

Options:

  • additionalProperties (boolean, default false) — allow properties beyond those observed in the samples.
  • existingSchema (JsonSchema & ObjectType) — fold a previously-generated schema into the merge as if it were another sample. Useful for widening a schema across runs.

Returns a JsonSchema & ObjectType. An empty data array yields an empty object schema.

createJsonSchemaValidator(schema)

Generate a function that validates an object against a JSON Schema.

import { createJsonSchemaValidator } from "data-to-jsonschema";

const validator = createJsonSchemaValidator({
    type: "object",
    properties: {
        id: { type: "number" },
        name: { type: "string" },
        age: { type: "number" },
        email: { type: "string" }
    },
    required: ["id", "name", "age", "email"]
});

validator({
    id: 1,
    name: "John Doe",
    age: 30,
    email: "myemail.com"
}); // { valid: true, errors: undefined }

validator({
    id: 1,
    age: null
}); // { valid: false, errors: [{...}] }

traverseObject(obj, callback)

Walk a plain javascript object and call a callback for each primitive leaf value, with its JSON-dot/bracket path and parent metadata.

import { traverseObject } from "data-to-jsonschema";

const obj = {
    id: 1,
    profile: {
        name: "John Doe",
    },
    tags: ["admin", "owner"],
};

traverseObject(obj, (value, jsonPath, meta) => {
    console.log(value, jsonPath, meta);
});

// console output (one line per primitive leaf):
// 1                 'id'             { key: 'id',      parent: { path: '',        type: 'object' } }
// 'John Doe'        'profile.name'   { key: 'name',    parent: { path: 'profile', type: 'object' } }
// 'admin'           'tags[0]'        { key: 0,         parent: { path: 'tags',    type: 'array'  } }
// 'owner'           'tags[1]'        { key: 1,         parent: { path: 'tags',    type: 'array'  } }

The third callback argument meta is optional — drop it if you only need the value and path.