0

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

2
  • but is not working – what's the error? Commented Sep 22, 2020 at 9:42
  • there is just logic error, I just need the right function Commented Sep 22, 2020 at 9:47

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I do not know is it normally, but I just created a simple function and deserialize through the loop

    def create(self, json: dict):
        key = Key()
        key.id = json.get("id")
        key.key_number = json.get("num")
        key.hash = json.get("hash")

        return key

1 Comment

Marschmallow has the advantage of a number of predefined fields and the possibility to validate them. See also the combination of marshmalllow and webargs. Especially as your project gets bigger it might help you. I am a friend of small, simple solutions and would be happy for you if your approach meets the expectation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.