json-schema-gen-cli
v1.0.2
Published
Generate JSON Schema from sample JSON data automatically
Maintainers
Readme
json-schema-gen-cli
Generate JSON Schema (Draft-7) from sample JSON data automatically. No external dependencies.
Install
npm install -g json-schema-gen-cliUsage
json-schema-gen <file|string> [options]
jsg <file|string> [options] # short aliasExamples
From a JSON file:
json-schema-gen sample.jsonSave schema to a file:
json-schema-gen sample.json --output schema.jsonFrom an inline JSON string:
json-schema-gen '{"name":"Alice","age":30}' --inlineMark all fields as required:
json-schema-gen sample.json --requiredAdd a title:
json-schema-gen sample.json --title "User"Output TypeScript interface instead:
json-schema-gen sample.json --typescriptSample Input/Output
Input (user.json):
{
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"name": "Alice",
"age": 30,
"score": 9.5,
"active": true,
"email": "[email protected]",
"homepage": "https://alice.dev",
"tags": ["admin", "user"],
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "12345"
},
"metadata": null
}Output (json-schema-gen user.json --title "User" --required):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "User",
"type": "object",
"properties": {
"id": { "type": "string", "format": "uuid" },
"name": { "type": "string" },
"age": { "type": "integer" },
"score": { "type": "number" },
"active": { "type": "boolean" },
"email": { "type": "string", "format": "email" },
"homepage": { "type": "string", "format": "uri" },
"tags": { "type": "array", "items": { "type": "string" } },
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"zip": { "type": "string" }
},
"additionalProperties": false
},
"metadata": { "type": "null" }
},
"required": ["id","name","age","score","active","email","homepage","tags","address","metadata"],
"additionalProperties": false
}TypeScript output (json-schema-gen user.json --typescript --title "User"):
export interface User {
id?: string;
name?: string;
age?: number;
score?: number;
active?: boolean;
email?: string;
homepage?: string;
tags?: string[];
address?: { street?: string; city?: string; zip?: string };
metadata?: null;
}Schema Inference Rules
| JSON Value | JSON Schema Type |
|--------------------|-------------------------------|
| "hello" | string |
| "2024-01-01" | string + format: date-time|
| "[email protected]" | string + format: email |
| "https://..." | string + format: uri |
| UUID string | string + format: uuid |
| 42 | integer |
| 3.14 | number |
| true/false | boolean |
| null | null |
| [...] | array (items inferred) |
| {...} | object (properties inferred)|
| Mixed array types | anyOf union |
Options
| Flag | Short | Description |
|---------------------|-------|------------------------------------------|
| --output <file> | -o | Write output to file |
| --inline | -i | Treat argument as inline JSON string |
| --required | -r | Mark all object properties as required |
| --title <name> | | Add title field to schema |
| --typescript | -t | Output TypeScript interface |
| --help | -h | Show help |
License
MIT © Wilson Xu
