Working with JSON Data in Terminal
JSON data validation and schema operations using JSON Schema standards.
Available Commands
Section titled “Available Commands”The fairspec data command provides utilities for working with JSON data:
validate- Validate JSON data against a Data Schema (JSON Schema)infer-schema- Automatically generate a Data Schema from JSON datavalidate-schema- Validate a Data Schema itselfinfer-format- Detect the format of a data file
Validate JSON Data
Section titled “Validate JSON Data”Validate JSON data files against a Data Schema (JSON Schema):
# Validate JSON data with a schemafairspec data validate data.json --schema schema.json
# Validate from a remote sourcefairspec data validate https://example.com/data.json --schema schema.json
# Output validation report as JSONfairspec data validate data.json --schema schema.json --jsonOptions
Section titled “Options”--schema <path>(required) - Path to a Data Schema descriptor (JSON Schema)--silent- Suppress output messages--debug- Show debug information--json- Output as JSON
Validation Report
Section titled “Validation Report”Returns a validation report with:
valid- Boolean indicating if validation passederrors- Array of validation errors (if any)
Example validation errors:
{ "valid": false, "errors": [ { "type": "data", "instancePath": "/users/0/email", "schemaPath": "#/properties/users/items/properties/email/format", "keyword": "format", "message": "must match format \"email\"" } ]}Example Usage
Section titled “Example Usage”Create a JSON Schema file (user-schema.json):
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "name": { "type": "string" }, "email": { "type": "string", "format": "email" }, "age": { "type": "integer", "minimum": 0 } }, "required": ["name", "email"]}Validate data against the schema:
fairspec data validate user.json --schema user-schema.jsonInfer Data Schema
Section titled “Infer Data Schema”Automatically generate a Data Schema (JSON Schema) from JSON data:
# Infer schema from local filefairspec data infer-schema data.json
# Infer schema from remote filefairspec data infer-schema https://example.com/data.json
# Save inferred schema to filefairspec data infer-schema data.json --json > schema.json
# Output for human readingfairspec data infer-schema data.jsonOptions
Section titled “Options”--silent- Suppress output messages--debug- Show debug information--json- Output as JSON
Generated Schema
Section titled “Generated Schema”The inferred schema will automatically detect:
- Data types (string, number, integer, boolean, null)
- Object structures and nested properties
- Array items and their types
- Required properties based on presence
- Enum values for properties with limited options
Example
Section titled “Example”Given this JSON data (users.json):
[ { "id": 1, "name": "Alice", "email": "alice@example.com", "age": 30, "active": true }, { "id": 2, "name": "Bob", "email": "bob@example.com", "age": 25, "active": false }]Infer the schema:
fairspec data infer-schema users.json --jsonGenerated schema:
{ "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" }, "email": { "type": "string" }, "age": { "type": "integer" }, "active": { "type": "boolean" } }, "required": ["id", "name", "email", "age", "active"] }}Validate Data Schema
Section titled “Validate Data Schema”Validate that a Data Schema (JSON Schema) file is valid:
# Validate a schema filefairspec data validate-schema schema.json
# Validate from remote sourcefairspec data validate-schema https://example.com/schema.json
# Output as JSONfairspec data validate-schema schema.json --jsonOptions
Section titled “Options”--silent- Suppress output messages--debug- Show debug information--json- Output as JSON
Schema Validation
Section titled “Schema Validation”This validates that the schema itself is:
- Valid JSON
- Compliant with JSON Schema Draft 2020-12 specification
- Has correct property definitions
- Uses valid keywords and formats
Validation Report
Section titled “Validation Report”{ "valid": true, "errors": []}Or if invalid:
{ "valid": false, "errors": [ { "type": "schema/invalid", "message": "Invalid schema property: 'typ' (did you mean 'type'?)" } ]}Infer Format
Section titled “Infer Format”Automatically detect the format of a data file:
# Infer format from filefairspec data infer-format data.json
# Infer from remote filefairspec data infer-format https://example.com/data.jsonl
# Output as JSONfairspec data infer-format data.json --jsonOptions
Section titled “Options”--sample-bytes <bytes>- Sample size in bytes for format detection--silent- Suppress output messages--debug- Show debug information--json- Output as JSON
Detected Formats
Section titled “Detected Formats”The command can detect:
json- Standard JSON formatjsonl- JSON Lines (newline-delimited JSON)
Example Output
Section titled “Example Output”{ "name": "json"}Or for JSONL:
{ "name": "jsonl"}Common Workflows
Section titled “Common Workflows”Create and Validate with Schema
Section titled “Create and Validate with Schema”# 1. Infer schema from existing datafairspec data infer-schema sample-data.json --json > data-schema.json
# 2. Validate new data against the schemafairspec data validate new-data.json --schema data-schema.json
# 3. Check if validation passedif [ $? -eq 0 ]; then echo "Data is valid!"else echo "Data validation failed"fiSchema-Driven Development
Section titled “Schema-Driven Development”# 1. Create a schema for your data structurecat > api-schema.json << 'EOF'{ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "users": { "type": "array", "items": { "type": "object", "properties": { "id": { "type": "integer" }, "username": { "type": "string", "minLength": 3 }, "email": { "type": "string", "format": "email" } }, "required": ["id", "username", "email"] } } }}EOF
# 2. Validate the schema itselffairspec data validate-schema api-schema.json
# 3. Validate API responses against the schemafairspec data validate response.json --schema api-schema.jsonAutomated Testing
Section titled “Automated Testing”# Validate data in a test scriptfor file in test-data/*.json; do echo "Validating $file..." if fairspec data validate "$file" --schema schema.json --silent; then echo "✓ $file is valid" else echo "✗ $file failed validation" exit 1 fidoneOutput Formats
Section titled “Output Formats”Text Output (default)
Section titled “Text Output (default)”Human-readable output with colors and formatting:
fairspec data validate data.json --schema schema.jsonOutput:
✓ Data is validOr with errors:
✗ Data validation failed
Errors: • /users/0/email: must match format "email" • /users/1/age: must be >= 0JSON Output
Section titled “JSON Output”Machine-readable JSON for automation and scripting:
fairspec data validate data.json --schema schema.json --jsonSilent Mode
Section titled “Silent Mode”Suppress all output except errors:
fairspec data validate data.json --schema schema.json --silentUse exit code to check success:
if fairspec data validate data.json --schema schema.json --silent; then echo "Valid"else echo "Invalid"fiExamples
Section titled “Examples”API Response Validation
Section titled “API Response Validation”# Fetch API response and validatecurl -s https://api.example.com/users > response.jsonfairspec data infer-schema response.json --json > api-schema.json
# Validate future responsescurl -s https://api.example.com/users | \ fairspec data validate /dev/stdin --schema api-schema.jsonConfiguration File Validation
Section titled “Configuration File Validation”# Create schema for config filescat > config-schema.json << 'EOF'{ "type": "object", "properties": { "host": { "type": "string" }, "port": { "type": "integer", "minimum": 1, "maximum": 65535 }, "ssl": { "type": "boolean" } }, "required": ["host", "port"]}EOF
# Validate config filefairspec data validate config.json --schema config-schema.jsonData Pipeline Validation
Section titled “Data Pipeline Validation”# Validate input datafairspec data validate input.json --schema input-schema.json
# Process data (your custom script)./process-data.sh input.json output.json
# Validate output datafairspec data validate output.json --schema output-schema.jsonSchema Evolution
Section titled “Schema Evolution”# Start with inferred schema from v1 datafairspec data infer-schema data-v1.json --json > schema-v1.json
# Manually update schema for v2 (add optional properties)# Edit schema-v1.json -> schema-v2.json
# Validate that v2 schema is still validfairspec data validate-schema schema-v2.json
# Ensure v1 data is still compatible with v2 schemafairspec data validate data-v1.json --schema schema-v2.json