Skip to main content

Convert json object to dataclass and vice versa

Project description

JTO Converter

Description

Convert json object to dataclass and vice versa.

Requirements

Required structure of dataclass field

All the parts of the below structure are required.

field_name: str = field(default=None, metadata={'name': 'json_field_name', 'required': False})
  • field_name can be any variable name.
  • field type should be strongly typed.
    For example in case of field containing the list it should look like this List[SomeClass]
  • default field's value in the most cases will be None, but it also can be changed.
  • name is the name of the field in original json.
  • required marked True if the field is required in the provided json.

Examples

Convert json object to class objects

from dataclasses import dataclass, field
from typing import List

from jto import JTOConverter

data = {
    "status": 200,
    "data": {
        "first": "qwer",
        "last": "qwer",
        "test": [
            {"f1": "1"},
            {"f1": "2"}
        ]
    }
}

@dataclass
class Test:
    f1: str = field(default=None, metadata={'name': 'f1', 'required': False})

@dataclass
class Data:
    first: str = field(default=None, metadata={'name': 'first', 'required': False})
    last: str = field(default=None, metadata={'name': 'last', 'required': False})
    test: List[Test] = field(default=None, metadata={'name': 'test', 'required': False})

@dataclass
class Response:
    status: int = field(default=None, metadata={'name': 'status', 'required': False})
    data: Data = field(default=None, metadata={'name': 'data', 'required': False})


dataclass_object = JTOConverter.from_json(Response, data)
print(dataclass_object)

dataclass_object.status = None
json_object = JTOConverter.to_json(dataclass_object, drop_empty_keys=True)
print(json_object)

Get class templates from json object

from jto.dataclass_generator import ClassesTemplate

data = {
    "status": 200,
    "data": {
        "first": "qwer",
        "last": "qwer",
        "test": [
            {"f1": "1"},
            {"f1": "2"}
        ]
    }
}

classes = ClassesTemplate()
classes.build_classes('Response', data)
print(classes)

classes_str = classes.build_classes_string()
print(classes_str)

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

jto-1.0.1.tar.gz (13.4 kB view hashes)

Uploaded Source

Built Distribution

jto-1.0.1-py3-none-any.whl (12.1 kB view hashes)

Uploaded Python 3

Supported by

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