JSON Schema Faker CLI
raw JSON →json-schema-faker-cli provides a command-line interface (CLI) for the json-schema-faker library, enabling users to generate realistic mock data based on JSON schemas directly from the terminal without requiring programmatic JavaScript interaction. It is currently at version 5.0.6 and primarily serves as a utility for quick data generation. The tool allows specifying a JSON schema file, an optional output file, the number of items for array generation, and a separate options file (JS or JSON format) to customize the faker's behavior. While json-schema-faker itself is a library for programmatic use, this package differentiates itself by offering a shell-based workflow, making it ideal for scaffolding mock APIs, testing, or populating databases with dummy data through simple CLI commands. Its release cadence is generally tied to the underlying json-schema-faker library's evolution, offering a stable and straightforward interface.
Common errors
error command not found: generate-json ↓
npm install -g json-schema-faker-cli or execute the command via npx for a temporary run: npx json-schema-faker-cli generate-json ... error SyntaxError: Unexpected token '{' in JSON at position 1 (or similar JSON parsing error) ↓
schema.json and options.json files using a linter or online JSON validator. Ensure they strictly adhere to JSON syntax rules. If using a JS options file, confirm it's valid CommonJS. error Error: Not a number: undefined (or similar type error related to array length argument) ↓
none placeholders correctly. For example, generate-json schema.json output.json 5 or generate-json schema.json none 5 options.js. Warnings
breaking This CLI currently depends on a specific release candidate range of `json-schema-faker` (`^0.5.0-rc.21`). This means it may not be compatible with newer major versions of the underlying `json-schema-faker` library (e.g., `0.6.x` or `0.7.x`) which can introduce breaking changes to options, faker providers, or schema interpretation. Users should ensure their expectations align with the features and behaviors of the `0.5.x-rc` series. ↓
gotcha When skipping optional parameters to provide a later one (e.g., providing an options file but no output file), you must explicitly use `none` as a placeholder for each skipped parameter. Forgetting this will cause subsequent parameters to be misinterpreted by the CLI. ↓
gotcha The options file can be either a plain JSON file or a CommonJS JavaScript file (`module.exports = { ... }`). If it's a JavaScript file, it must conform to CommonJS syntax, not ES Module (`export default { ... }`) syntax, as Node.js in the context of this CLI will load it as CommonJS. ↓
gotcha Outputting generated JSON to `stdout` (by omitting the output file path) is useful for piping to other commands, but can be problematic if the schema generates a very large JSON output, potentially flooding the terminal, causing performance issues, or making inspection difficult. ↓
Install
npm install json-schema-faker-cli yarn add json-schema-faker-cli pnpm add json-schema-faker-cli Imports
- generate-json wrong
import { generateJson } from 'json-schema-faker-cli'correctgenerate-json schema.json output.json - Options file (JS) wrong
export default { minLength: 20 };correct// options.js module.exports = { minLength: 20 }; - Options file (JSON)
// options.json { "minLength": 20 }
Quickstart
npm install -g json-schema-faker-cli
# Create a JSON schema file (e.g., schema.json)
cat > schema.json <<EOF
{
"type": "object",
"properties": {
"id": { "type": "integer", "format": "uuid" },
"name": { "type": "string", "faker": "name.findName" },
"email": { "type": "string", "format": "email" },
"createdAt": { "type": "string", "format": "date-time" }
},
"required": ["id", "name", "email"]
}
EOF
# Create an optional JavaScript options file (e.g., options.js)
cat > options.js <<EOF
module.exports = {
minLength: 10,
failOnInvalidFormat: false,
random: () => 0.5 // Ensures consistent output for demo
};
EOF
echo "\n--- Generating a single object to stdout ---"
generate-json schema.json
echo "\n--- Generating a single object to 'output.json' ---"
generate-json schema.json output.json
echo "\n--- Generating an array of 3 objects to 'output-array.json' ---"
generate-json schema.json output-array.json 3
echo "\n--- Generating with custom options from 'options.js' to 'output-with-options.json' ---"
generate-json schema.json output-with-options.json none options.js
# Clean up generated files
rm schema.json output.json output-array.json output-with-options.json options.js