Skip to content

MC769/JSON-to-Python-Dataclasses

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

🚀 json2py - JSON to Python Dataclasses

Stop writing Python classes by hand. Generate them from JSON in 1 second.

Python Version

⚡ Quick Demo

$ echo '{"name": "John", "age": 30, "email": "john@example.com"}' > user.json
$ python json2py.py user.json --output user.py

✅ Generated: user.py
📦 Root class: User
📝 Lines of code: 28

🎯 Why Use This?

Without json2py With json2py
❌ Manual class writing ✅ Automatic generation
❌ Easy to miss fields ✅ Complete type hints
❌ No nested class handling ✅ Auto-creates nested classes
❌ 10 minutes of work ✅ 1 second

📦 Installation

# Clone and run locally
git clone https://github.com/YOUR_USERNAME/json2py
cd json2py
python json2py.py input.json

🚀 Usage

Basic Usage

# Generate from JSON file
python json2py.py response.json

# Specify output file
python json2py.py response.json --output models.py

# Use with API response
curl https://api.github.com/users/octocat | python json2py.py --output github_user.py

✨ Features

Feature Status
🏗️ Dataclasses
🔄 Nested Objects
📝 Type Hints
📅 DateTime Detection
📋 Lists
🔧 from_dict() method
📤 to_dict() method

📖 Example

Input JSON (sample.json):

{
  "user_id": 123,
  "full_name": "John Doe",
  "email": "john@example.com",
  "is_active": true,
  "profile": {
    "avatar_url": "https://example.com/avatar.jpg",
    "bio": "Python developer",
    "followers": 1234
  },
  "tags": ["python", "opensource", "developer"]
}

Run command:

python json2py.py sample.json --output models.py

Generated Python (models.py):

from dataclasses import dataclass
from typing import List

@dataclass
class Profile:
    avatar_url: str
    bio: str
    followers: int

    @classmethod
    def from_dict(cls, data: dict):
        return cls(**data)
    
    def to_dict(self):
        return {k: v for k, v in self.__dict__.items() if v is not None}

@dataclass
class Sample:
    user_id: int
    full_name: str
    email: str
    is_active: bool
    profile: Profile
    tags: List[str]

    @classmethod
    def from_dict(cls, data: dict):
        return cls(**data)
    
    def to_dict(self):
        return {k: v for k, v in self.__dict__.items() if v is not None}

Use in your code:

from models import Sample
import json

# Load JSON
with open('response.json') as f:
    data = json.load(f)

# Convert to Python object
user = Sample.from_dict(data)
print(user.full_name)  # John Doe
print(user.profile.bio)  # Python developer

# Convert back to dict
user_dict = user.to_dict()

🛠️ Development

Run Locally

[git clone https://github.com/MC769/json2p](https://github.com/MC769/JSON-to-Python-Dataclasses.git)
cd json2py
python json2py.py examples/sample.json

🤝 Contributing

  1. Fork the repo
  2. Create a branch (git checkout -b feature/amazing)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push (git push origin feature/amazing)
  5. Open a Pull Request

⭐ Show Your Support

If this saved you time, please star the repo!

Star on GitHub

About

⚡ Convert JSON to Python dataclasses instantly. Stop writing boilerplate. Auto-generates nested classes, type hints, from_dict() and to_dict() methods. Save 10 minutes every API integration.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages