Utilities for JSON schema extraction and Pydantic model generation
Project description
API to Pydantic Utils
Core utilities for JSON schema extraction and Pydantic model generation
A Python package that provides utilities for processing JSON data and extracting schemas for Pydantic model generation. Originally developed as part of the API to Pydantic project.
Installation
pip install api-to-pydantic-utils
Features
- Schema Extraction: Intelligent JSON schema extraction with 80-95% compression
- Type Detection: Automatic detection of emails, URLs, timestamps, and other patterns
- Model Testing: Utilities for validating Pydantic models against JSON data
- JSON Validation: Simple JSON file validation utilities
Quick Start
from api_to_pydantic_utils import extract_schema, validate_json_file, create_test_file
# Extract schema from JSON data
schema = extract_schema({"name": "John", "email": "john@example.com"})
print(schema)
# Validate a JSON file
is_valid = validate_json_file("data.json")
# Create test file for a Pydantic model
test_file = create_test_file("model.py", "test_data.json")
API Reference
extract_schema(data, max_depth=10)
Extracts a compressed schema from JSON data.
Parameters:
data: JSON data (dict, list, or primitive)max_depth: Maximum nesting depth to process
Returns: Compressed schema representation
process_json_file(file_path, max_depth=10)
Processes a JSON file and extracts its schema.
Parameters:
file_path: Path to JSON filemax_depth: Maximum nesting depth to process
Returns: Tuple of (schema, compression_stats)
validate_json_file(file_path)
Validates if a file contains valid JSON.
Parameters:
file_path: Path to JSON file
Returns: Boolean indicating if file is valid JSON
create_test_file(model_file_path, json_file_path)
Creates a test file for validating Pydantic models.
Parameters:
model_file_path: Path to Python file with Pydantic modeljson_file_path: Path to JSON file to test against
Returns: Path to created test file
Type Detection
The package automatically detects various data patterns:
- Email addresses:
john@example.com→EmailStr - URLs:
https://example.com→HttpUrl - Timestamps:
2023-01-01T00:00:00Z→datetime - YouTube IDs: Channel and video ID patterns
- Usernames:
@usernamepatterns - Text content: Long text with punctuation
Requirements
- Python 3.8+
- Pydantic 2.0+
License
MIT License
Contributing
This package is part of the API to Pydantic project. Please report issues and contribute there.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file api_to_pydantic_utils-0.1.1.tar.gz.
File metadata
- Download URL: api_to_pydantic_utils-0.1.1.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dedccb9ae7eba817254404fcc37af6a30bef3ebb9f9abdbee0c128a281181bc1
|
|
| MD5 |
fa75c52817b17f9a69607a548945112f
|
|
| BLAKE2b-256 |
83a0bf69b494272ef383a91564eeb6b9e15563687001fd02fdd55250b47e4443
|
File details
Details for the file api_to_pydantic_utils-0.1.1-py3-none-any.whl.
File metadata
- Download URL: api_to_pydantic_utils-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22d17af20cbab6f2503481be82c8aed32b2a20f4c36e35c2ceb86c7a7e9d4cf5
|
|
| MD5 |
5de0c75ae762b5587f839136d50d2cd0
|
|
| BLAKE2b-256 |
381877314d8bf5f16518a172cf97ca007f3a2f9289f4bc9ef5282ca467f84b3c
|