@lxgicstudios/json-to-schema
v1.0.0
Published
Generate JSON Schema (draft-07) from sample JSON data. Handles nested objects, arrays, type detection, and schema merging. CLI tool.
Maintainers
Readme
json-to-schema
Generate JSON Schema (draft-07) from sample JSON data. It handles nested objects, arrays, type detection, and can merge multiple samples into one schema.
Install
npm install -g @lxgicstudios/json-to-schemaOr run directly:
npx @lxgicstudios/json-to-schema data.jsonFeatures
- Draft-07 output - Generates valid JSON Schema draft-07
- Type inference - Detects string, number, integer, boolean, null, object, array
- Format detection - Recognizes date-time, email, URI, UUID, IPv4, IPv6
- Schema merging - Combine multiple samples into one schema
- Required fields - Mark all fields as required with one flag
- Nested support - Handles deeply nested objects and arrays
- JSONL support - Process multiple JSON objects per file
- Pipe-friendly - Works great with stdin/stdout
- Colorized output - Pretty terminal output with syntax highlighting
- Zero dependencies - Built with Node.js builtins only
Usage
# From a file
json-to-schema data.json
# From stdin
echo '{"name":"John","age":30,"email":"[email protected]"}' | json-to-schema
# Merge multiple samples
json-to-schema --merge -f sample1.json -f sample2.json
# All fields required
json-to-schema --required data.json
# With title and description
json-to-schema --title "User" --description "A user object" data.json
# Pipe from an API
curl -s https://api.example.com/users/1 | json-to-schema --title "User"
# Compact output
json-to-schema --compact data.jsonOptions
| Option | Description | Default |
|--------|-------------|---------|
| -f, --file <path> | Input JSON file (repeatable) | - |
| --merge | Merge schemas from multiple samples | false |
| --required | Mark all fields as required | false |
| --title <title> | Schema title | - |
| --description <desc> | Schema description | - |
| --additional | Allow additional properties | false |
| --no-format | Skip format detection | false |
| --compact | No indentation in output | false |
| --help | Show help | - |
Example Output
Input:
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"active": true,
"tags": ["admin", "user"]
}Output:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"email": { "type": "string", "format": "email" },
"active": { "type": "boolean" },
"tags": {
"type": "array",
"items": { "type": "string" }
}
},
"additionalProperties": false
}Built by LXGIC Studios
