Registry / devops / json-schema-generator

json-schema-generator

JSON →
library2.0.6jsnpmunverified

A CLI and library for generating JSON Schema (draft-v4) from JSON documents. Version 2.0.6 is the latest stable release. It supports local files, URLs, and stdin input with options for pretty-printing and directory output. Note: full draft-v4 spec not yet supported; lacks $ref and location nodes. Alternative to tools like jsonschema or online generators. Low release cadence; GitHub last updated several years ago.

npm install json-schema-generator
INSTALL
IMPORT
SIG · JSON-SCHEMA-GENERA
J
json-schema-generator
devopsjavascriptv2.0.6
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

jsonSchemaGenerator
const jsonSchemaGenerator = require('json-schema-generator');
import jsonSchemaGenerator from 'json-schema-generator';
Package provides CommonJS module only; no ESM or TypeScript types.
jsonSchemaGenerator (as default)
const jsonSchemaGenerator = require('json-schema-generator'); const schema = jsonSchemaGenerator({foo: 'bar'});
const { jsonSchemaGenerator } = require('json-schema-generator');
The module exports a single function directly, not an object.
CLI invocation
json-schema-generator input.json -o schema.json
json-schema-generator --file input.json -o schema.json
The --file option is available but unnecessary; passing a path directly works.

Demonstrates generating a JSON Schema from a JavaScript object and saving to file.

const jsonSchemaGenerator = require('json-schema-generator'); const input = { apple: { color: 'red', quantity: 5 } }; const schema = jsonSchemaGenerator(input); const fs = require('fs'); fs.writeFileSync('schema.json', JSON.stringify(schema, null, 2)); console.log('Generated schema written to schema.json');
Debug
Known issues
gotchaGenerated schema is draft-04, not draft-07 or 2020-12. Many modern validators expect newer drafts.
fix
Use a newer generator like @jsonschema/json-schema-generator or use a migration tool.
affects: <=2.0.6
gotchaNo support for $ref. Generated schemas are fully expanded, which can produce very large files.
fix
Manually refactor large schemas or use a tool that supports refs.
affects: <=2.0.6
gotchaEmpty object {} produces schema with no properties. This may be valid but often unexpected.
fix
Ensure input object has at least one property if non-empty schema is desired.
affects: <=2.0.6
Errors
Common errors & fixes
TypeError: jsonSchemaGenerator is not a function
Using named import from ES module syntax when package exports as default function.
fix
Use const jsonSchemaGenerator = require('json-schema-generator');
SyntaxError: Unexpected token '.' when running as CLI
Spaces in file path not properly escaped.
fix
Wrap path in quotes: json-schema-generator "path/with spaces/input.json"
ENOENT: no such file or directory when using --schemadir
Output directory does not exist; tool does not create directories.
fix
Create the directory first: mkdir -p schemas && json-schema-generator input.json -o schemas/
Upgrade
Version history
2.0.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Resources
json-schema-generator — npm install json-schema-generator · libregistry