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

json-learner

v0.1.0

Published

Infer and merge JSON Schemas (draft-07) from JSON data — like Python's genson, for Node.js

Readme

json-learner

Infer and merge JSON Schemas (draft-07) from JSON data — like Python's genson, for Node.js.

Features

  • Infer JSON Schema from one or more JSON files or stdin
  • Merge/refine an existing schema with new data
  • Strict mode: read-only linter — validates JSON against a schema, exits with code 1 on violations (CI-friendly)
  • Moderate mode: validates JSON, reports violations as JSON Lines, and updates the schema — exits 0 always
  • Generate markdown documentation via @adobe/jsonschema2md
  • Usable as a library (npm install json-learner)

Installation

npm install -g json-learner
# or use via npx
npx json-learner --help

CLI usage

json-learner [options] [files...]

Options:
  --schema <file>     Existing schema to refine or validate against
  --output <file>     Write output schema to file (default: stdout)
  --strict            Validate only (read-only); exit 1 on violations [requires --schema]
  --moderate          Validate and refine; emit violations as JSON Lines to stdout [requires --schema]
  --document [dir]    Generate markdown docs into directory (default: ./docs)
  --format            Pretty-print output JSON
  -V, --version       Show version
  -h, --help          Show help

Generate a schema from data

# From stdin
echo '{"name":"Alice","age":30}' | json-learner

# From file(s)
json-learner users.json

# Multiple files are merged into one schema
json-learner users-jan.json users-feb.json --output schema.json --format

Refine an existing schema with new data

If a field is missing in any new record, it becomes optional:

json-learner --schema schema.json new-records.json --output schema.json

Strict mode (linter / CI)

Exits with code 1 if any record violates the schema. Violations go to stderr.

json-learner --schema schema.json --strict data.json
echo $?  # 1 if violations, 0 if valid

Moderate mode (reporter + learner)

Reports violations as JSON Lines to stdout and refines the schema with new data. Always exits 0.

json-learner --schema schema.json --moderate data.json --output schema.json

# Pipe violations to jq
json-learner --schema schema.json --moderate data.json | grep '"keyword"' | jq .

Generate documentation

json-learner --schema schema.json --document ./docs
# Writes ./docs/schema.md

The --document flag is independent and can be combined with any other mode.

Library usage

import { inferSchema, inferFromMany, mergeSchemas, validate } from 'json-learner'

// Infer from a single value
const schema = inferSchema({ name: 'Alice', age: 30 })

// Infer from multiple values (required = intersection)
const schema2 = inferFromMany([
  { name: 'Alice', age: 30 },
  { name: 'Bob' },            // age missing → becomes optional
])

// Refine existing schema with new data
const updated = mergeSchemas(existingSchema, { name: 'Carol' })

// Validate
const result = validate(schema, { name: 42 })
if (!result.valid) {
  console.log(result.violations)
}

How it works

json-learner uses a strategy pattern inspired by genson:

  • Each JSON type (null, boolean, integer, number, string, array, object) has a dedicated strategy
  • A SchemaNode accumulates strategies and merges them
  • required fields are computed as the intersection across all observed objects — a field is only required if it appears in every single record
  • Integer values are promoted to number when a float is encountered

Requirements

  • Node.js >= 20

License

MIT