JSON to Pydantic Skill
This skill helps convert raw JSON data or API responses into structured, strongly-typed Python classes using Pydantic.
Instructions
Analyze the Input: Look at the JSON object provided by the user.
Infer Types:
string->strnumber->intorfloatboolean->boolarray->List[Type]null->Optional[Type]- Nested Objects -> Create a separate sub-class.
Follow the Example: Review
examples/to see how to structure the output code. notice how nested dictionaries likepreferencesare extracted into their own class.- Input:
examples/input_data.json - Output:
examples/output_model.py
- Input:
Style Guidelines
- Use
PascalCasefor class names. - Use type hints (
List,Optional) fromtypingmodule. - If a field can be missing or null, default it to
None.
Outputs & Deliverables
- Primary Output: Python Pydantic models with complete type annotations
- Secondary Output: Validation rules and field defaults
- Success Criteria: Models match JSON structure and include proper type hints
- Quality Gate: Ready for integration into implementer's codebase
Constraints
- NO business logic. Data models only.
- NO implementation code beyond model definitions.
- Must handle nested objects and optional fields correctly.
Common Pitfalls
- Ignoring Nested Structures: Not extracting nested objects into separate classes creates monolithic models. Always decompose; create sub-classes for nested dicts.
- Wrong Type Inference: Confusing
nullwith missing fields.null=Optional, missing entirely = Field withdefault_factory. Be precise. - Generic Field Names: Using
data,value,resultinstead of domain-specific names. Use the field name from JSON. - Skipping Validation: Not adding constraints like
Field(min_length=1)or regex patterns. Add validation rules to the model. - Mixing Array Element Types: Not using
Uniontypes when arrays can contain multiple types. Use generics correctly. - Missing Aliases: Not mapping JSON camelCase to Python snake_case with Field aliases. Handle naming convention mismatches explicitly.
Integration Points
| Phase | Input From | Output To | Context |
|---|---|---|---|
| Input | JSON response or API data | Model generation | Analyze JSON structure and infer types |
| Decomposition | Nested structures | Sub-class extraction | Create separate Pydantic models for objects |
| Integration | Generated models | implementer |
Use in service layer for validation |
| Validation | Model with constraints | Runtime protection | Pydantic validates all incoming data |
Converted and distributed by TomeVault — claim your Tome and manage your conversions.