I have json with data about my object I created schema for serialize it into list of objects, but it isn't work
Schema:
from marshmellow import fields, Schema
class ContactSchema(Schema):
first_name = fields.String(attribute="ftNm")
last_name = fields.String(attribute="ltNm")
phone = fields.Integer(attribute="pn")
Model:
class Contact:
id: int
first_name: str
last_name: str
phone: str
And i have a function to convert (but is not working)
def json_to_list():
json = [{'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
{'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
{'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}]
schema = ContactSchema()
result = schema.dump(json)
I will appreciate if someone help me with function to convert json to list of objects
I'm not exactly sure what your intentions are. However, serialization and deserialization are possible in the following ways.
Serialization and deserialization with renaming of the attributes specified by the variable names.
from marshmallow import Schema, fields
class ContactSchema(Schema):
first_name = fields.Str(attribute="ftNm", data_key="ftNm")
last_name = fields.Str(attribute="ltNm", data_key="ltNm")
phone = fields.Integer(attribute="pn", data_key="pn")
# serialization to json
def from_list():
data = [
{'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
{'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
{'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}
]
schema = ContactSchema(many=True)
return schema.dump(data)
# deserialization from json
def to_list():
json = [
{'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
{'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
{'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}
]
schema = ContactSchema(many=True)
return schema.load(json)
Deserialization without renaming of the attributes specified by the variable names.
class ContactSchema(Schema):
first_name = fields.Str(attribute="ftNm")
last_name = fields.Str(attribute="ltNm")
phone = fields.Integer(attribute="pn")
# deserialization from json
def to_list():
json = [
{'first_name': 'Name1', 'last_name': 'Surname1', 'phone': 343434},
{'first_name': 'Name2', 'last_name': 'Surname2', 'phone': 141414},
{'first_name': 'Name3', 'last_name': 'Surname3', 'phone': 656565}
]
schema = ContactSchema(many=True)
return schema.load(json)
The direction of the conversion may not be indicated correctly.
from marshmallow import Schema, fields, post_load
from dataclasses import dataclass
@dataclass
class Contact:
# id: int
first_name: str
last_name: str
phone: str
class ContactSchema(Schema):
first_name = fields.Str(data_key="ftNm")
last_name = fields.Str(data_key="ltNm")
phone = fields.Integer(data_key="pn")
@post_load
def make_user(self, data, **kwargs):
return Contact(**data)
# deserialization from json
def to_list():
json = [
{'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
{'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
{'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}
]
schema = ContactSchema(many=True)
return schema.load(json)
To serialize and deserialize a database model, I recommend flask-marshmallow and marshmallow-sqlalchemy.