On this page
Command Line Interface
Use jsf from the command line to generate JSON data from schema files.
Installation
Install globally to use the jsf command anywhere.
npm install -g json-schema-faker
Basic Usage
Generate JSON from a schema file. Output to stdout or save to a file with -o.
# Generate to stdout
jsf schema.json
# Save to file
jsf schema.json -o output.json
# Generate 10 items as array
jsf schema.json -c 10
CLI Options
Control generation with command-line flags.
# Deterministic output with seed
jsf schema.json --seed 42
# Include all optional properties
jsf schema.json --all-optionals
# Override array/string constraints
jsf schema.json --min-items 5 --max-length 20
# Use schema defaults and examples
jsf schema.json --use-defaults --use-examples
# Read from stdin
cat schema.json | jsf -
# Combine options
jsf schema.json -o output.json -c 5 --seed 42
Available Flags
-o, --output <file> Output file-c, --count <n> Array count--options <file> Options JSON--seed <n> Random seed--max-depth <n> Max recursion--min-items <n> Min array items--max-items <n> Max array items--min-length <n> Min string length--max-length <n> Max string length--optionals <0-1> Probability--all-optionals Include all--use-defaults Use defaultsProgrammatic Usage
Import what you need using named imports:
import {
generate,
createGenerator,
registerFormat,
define,
reset,
} from "json-schema-faker";
API
generate(schema, options?)
Main entry point. Returns a Promise<unknown>.
const value = await generate({ type: "string", format: "email" });
createGenerator(options?)
Creates a reusable generator with auto-incrementing seeds.
const gen = createGenerator({ seed: 42 });
const a = await gen.generate(schema); // seed 42
const b = await gen.generate(schema); // seed 43
registerFormat(name, generator)
Register a custom format globally.
registerFormat("phone", (random) => {
const area = random.int(200, 999);
const num = random.int(1000000, 9999999);
return `+1-${area}-${num}`;
});
createRemoteResolver(options?)
Create a resolver for external $ref schemas (HTTP URLs or local files).
import { createRemoteResolver } from 'json-schema-faker';
const resolver = createRemoteResolver({
baseUrl: 'https://example.com/schemas/'
});
await generate(schema, { refResolver: resolver });
Available Options
Basic usage with options:
const result = await generate(schema, {
seed: 42,
maxDepth: 5,
alwaysFakeOptionals: true
});
Options summary:
interface GenerateOptions {
seed?: number; // Deterministic generation
maxDepth?: number; // Limit nesting depth
maxDefaultItems?: number; // Default: 3
optionalsProbability?: number; // Default: 0.5
alwaysFakeOptionals?: boolean; // Always include optionals
fillProperties?: boolean; // Fill all properties
minItems?: number; // Override minItems
maxItems?: number; // Override maxItems
minLength?: number; // Override minLength
maxLength?: number; // Override maxLength
useDefaultValue?: boolean; // Use schema defaults
useExamplesValue?: boolean; // Use examples array
formats?: Record<string, Function>; // Custom formats
refResolver?: RefResolver; // Custom $ref resolver
refDepth?: number; // Set both min/max
refDepthMin?: number; // Minimum recursion
refDepthMax?: number; // Maximum recursion
resolveJsonPath?: boolean; // Enable JSONPath
extensions?: { faker?: any; chance?: any };
minDateTime?: string; // ISO 8601 date
maxDateTime?: string; // ISO 8601 date
pruneProperties?: string[]; // Properties to remove
failOnInvalidTypes?: boolean; // Default: false
defaultInvalidTypeProduct?: unknown; // Fallback value
validateSchemaVersion?: boolean; // Check $schema
fixedProbabilities?: boolean; // Deterministic
propAliases?: Record<string, string>; // Remap schema keys
}Seeding & Randomness
Control randomness for reproducible results.
Depth Control
Limit nested structure generation.
Array Generation
Override array constraints.
String Generation
Override string length constraints.
Optional Properties
Control generation of optional schema properties.
Defaults & Examples
Use schema-defined default values and examples.
Custom Formats
Register custom format generators.
References & Resolution
Handle $ref and external schema resolution.
External Extensions
Use Faker.js or Chance.js for generation.
Custom Keywords
Define custom keyword generators to extend schema processing.
Built-in Keywords
JSON Schema Faker includes built-in keywords for common patterns.
Date & Time
Configure date-time format constraints.
Advanced Options
Error handling, pruning, and schema validation.