A tool to generate Python classes with type hints from JSON files
Project description
JSON to Python Class Generator
A tool that converts JSON files into Python classes with type hints, making it easier to work with structured JSON data in Python.
Features
- Converts JSON structures to Python classes with proper type annotations
- Handles nested objects and arrays
- Maintains proper naming conventions (camelCase to snake_case, etc.)
- Generates
__init__method for object initialization - Includes
__call__method to convert back to dictionary
Installation
From Source
Clone the repository
git clone https://github.com/yourusername/json2pyclass.git
cd json2pyclass
Install with pip
pip install .
For development (includes testing dependencies)
pip install ".[dev]"
Usage
Command Line Interface
Basic usage
json2pyclass input.json -o output.py
If output path is not specified, it will use input filename with .py extension
json2pyclass data.json
As a Library
from json2pyclass import generate_type_declare_file
Generate Python classes from JSON file
generate_type_declare_file("input.json", "output.py")
Example
For a JSON file like this: { "userName": "john_doe", "age": 30, "isActive": true, "address": { "street": "123 Main St", "city": "Anytown" }, "hobbies": ["reading", "hiking"] } The tool will generate a Python file with classes: from typing import List, Dict, Any, Optional
class Address: street: str city: str
def __init__(self, data: dict):
self.street = data.get('street')
self.city = data.get('city')
pass
def __call__(self) -> dict:
result = {}
result['street'] = self.street
result['city'] = self.city
return result
class User: user_name: str age: int is_active: bool address: Address hobbies: List[str]
def __init__(self, data: dict):
self.user_name = data.get('userName')
self.age = data.get('age')
self.is_active = data.get('isActive')
self.address = Address(data.get('address', {}))
self.hobbies = data.get('hobbies', [])
pass
def __call__(self) -> dict:
result = {}
result['userName'] = self.user_name
result['age'] = self.age
result['isActive'] = self.is_active
result['address'] = self.address() if self.address else None
result['hobbies'] = self.hobbies
return result
Testing
Run the test suite with: pytest
License
This project is licensed under the MIT License - see the LICENSE file for details.
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 json2pytype-0.1.1.tar.gz.
File metadata
- Download URL: json2pytype-0.1.1.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
174dbe11e9785e571650b31cfccb8d2d8d9993541fa6ec4352edd2e0fa82e13c
|
|
| MD5 |
31054d05ea37119941d1c5c940abb2dd
|
|
| BLAKE2b-256 |
e9f8c16e4be50b15a45422c98c5369be1b513540575df76db840a889b8889269
|
File details
Details for the file json2pytype-0.1.1-py3-none-any.whl.
File metadata
- Download URL: json2pytype-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abddcc84ac023bcdd314c1501453b655c3ceb9893716674c87be67036d340a6e
|
|
| MD5 |
c78e34ea3b1bac8b260a414a685b1f79
|
|
| BLAKE2b-256 |
b9f83aad1e931ae4e5fbd9fb56ecc03b671b19ae80793a552a507f60a8748744
|