[Flask] TypeError: xxxx is not JSON serializable

David Nieder davidnieder at gmx.de
Fri Nov 13 10:27:58 EST 2015

On 11.11.2015 18:16, Jim Icenhower via Flask wrote:
> Hi,
> I'm using Flask Appbuilder which seems to provide it's own REST implemention instead of using Flask-Rest.I have the following class. When I call it's view's api/read function I get the error "TypeError: TXABCD2015 is not JSON serializable". Note that TXABCD2015 is the value returned by the repr() method of the related Event object. It throws this error on all objects related through foreign keys. I really would like the Event repr() value to be included in the json if possible. I would appreciate any advice on what should I do to handle this.Thanks!class CarClass(Model,ModelEnhancer):
>      __tablename__ = "CarClass"
>      CarClassID = Column(Integer, primary_key=True)
>      CarClassKey = Column(String(30), unique=True, nullable=False)
>      Marque = Column(String(30), nullable=False)
>      Title = Column(String(30), nullable=False)
>      Description = Column(Text(1000), nullable=False)
>      StartYear = Column(Integer, nullable = True)
>      EndYear = Column(Integer, nullable = True)
>      EventID = Column(Integer, ForeignKey('event.EventID'), nullable=False)
>      Event = relationship('Event')
>      def setValues(self, row):
>          self.validationMessages = ""
>          for key, value in row.iteritems():
>              setattr(self, key, value)
>      def list_mapped_columns(self):
>          list_obj_mapped_columns(self)
>      def isValid(self):
>          validationMessage = ""
>          columns = self.required_column_names
>          for name in columns:
>              print name
>              if not getattr(self, name):
>                  # Add validation message
>                  self.validationMessages = self.validationMessages + ", " + name + " can not be empty."
>          return len(self.validationMessages) == 0
>      def __repr__(self):
>          return self.CarClassKey Jim

Hi Jim,
I don't know Flask-Appbuilder at all but I gave the code a glance and it 
seems like flask's jsonify method is used to generate json.
Flask extends the json module and calls an __html__ method if your 
object has one (see link below). So you could try adding sth like this 
to the objects in question:

def __html__(self):
     return repr(self)



More information about the Flask mailing list