title: Flask sqlalchemy save list as json
body: <p>I'm using this model in Flask-rest with sqlalchemy in python3.5</p>
  
  <pre><code>class UserModel(db.Model):
      id = db.Column(db.Integer, primary_key=True, autoincrement=True)
      username = db.Column(db.String(255), unique=True, nullable=False)
      password = db.Column(db.String(600), nullable=False)
      roles = db.Column(db.String(255), nullable=False)
  </code></pre>
  
  <p>I want to save in roles a string in json with a list of strings (the roles). I would like to access this attribute as if it were a python list and not a string (but save it in database as a string, a json list).</p>
  
  <p><strong>Non acceptable options:</strong></p>
  
  <p>1) I could make the conversion from and to database using getters and setters, but it's not what I'm looking for. It's strange to access every attribute directly except for the roles with getters and setters.</p>
  
  <p>2) Make it a relation with another table of roles. I want to keep the number of tables at minimum.</p>
  
  <hr>
  
  <p>Is there a way to add a method/something to add this process of transforming from and to json automatically when retrived from and saved to database?</p>