Skip to main content

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 file
  • max_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 model
  • json_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.comEmailStr
  • URLs: https://example.comHttpUrl
  • Timestamps: 2023-01-01T00:00:00Zdatetime
  • YouTube IDs: Channel and video ID patterns
  • Usernames: @username patterns
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

api_to_pydantic_utils-0.1.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

api_to_pydantic_utils-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

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

Hashes for api_to_pydantic_utils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 dedccb9ae7eba817254404fcc37af6a30bef3ebb9f9abdbee0c128a281181bc1
MD5 fa75c52817b17f9a69607a548945112f
BLAKE2b-256 83a0bf69b494272ef383a91564eeb6b9e15563687001fd02fdd55250b47e4443

See more details on using hashes here.

File details

Details for the file api_to_pydantic_utils-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for api_to_pydantic_utils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 22d17af20cbab6f2503481be82c8aed32b2a20f4c36e35c2ceb86c7a7e9d4cf5
MD5 5de0c75ae762b5587f839136d50d2cd0
BLAKE2b-256 381877314d8bf5f16518a172cf97ca007f3a2f9289f4bc9ef5282ca467f84b3c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page