Skip to main content

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


Download files

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

Source Distribution

json2pytype-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

json2pytype-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file json2pytype-0.1.0.tar.gz.

File metadata

  • Download URL: json2pytype-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for json2pytype-0.1.0.tar.gz
Algorithm Hash digest
SHA256 348c314291773195dc670392039a687a9aec0db3efc8abd4b5c64220ed00413c
MD5 59065450c685dea24c38db2ec25a6840
BLAKE2b-256 651178d4d4217dd5603d42e6ae2934a8cbdafceb7eb6fb4fc6e1334b626452e3

See more details on using hashes here.

File details

Details for the file json2pytype-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: json2pytype-0.1.0-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

Hashes for json2pytype-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a3a09bf38424f14f54ebacc2308dd505b46057201c5749a6063415beed69fc9
MD5 d289e006914f4c426cf3cf435a04c652
BLAKE2b-256 39e995e99bf765270bffc239940b33004ec6e6505c9dc3fbcb019070cb3e51cd

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