TypeError: Object of type is not JSON serializable TypeError: Object of type is not JSON serializable flask flask

TypeError: Object of type is not JSON serializable


In order to use jsonify() you have to make serializable the class you need to jsonify. Add to that class a function similar to the following:

class Version(db.Model):    __tablename__ = 'versions'    id = db.Column(db.String(), primary_key=True)    file = db.Column(db.String(80), nullable=True)    application_id = db.Column(db.Integer, db.ForeignKey('applications.id'))    def serialize(self):        return {"id": self.id,                "file": self.file,                "application_id": self.application_id}

And then jsonify de serializaed version of the object, and not the objetc itself:

jsonify(result.serialize())


You probably want to use ModelSchema instead of Schema.

class ApplicationDetailSchema(ma.ModelSchema):    class Meta:        model = Application        fields = ('id', 'name', 'versions')

ModelSchema dumps the related foreign key objects as a list of id(s) by default which is JSON serializable.