title: How to jsonify objects from sqlalchemy?
body: <p>I'm using Flask, SQLAlchemy and javascript. I need to pass the results of my query to javascript in the json format, through AJAX, but I keep getting this error:</p>
  
  <pre><code>raise TypeError(repr(o) + " is not JSON serializable")
  TypeError: &lt;model.Cloud object at 0x7f72e40c5910&gt; is not JSON serializable'
  </code></pre>
  
  <p>My code is as follows:</p>
  
  <pre><code>@app.route('/past/&lt;user_id&gt;')
  def get_past_clouds(user_id):
      if user_id:
          clouds = model_session.query(model.User).filter_by(id=user_id).first().clouds
          if clouds != "":
              clouds_d = {}
              for idx, cloud in enumerate(clouds):
                  clouds_d[idx] = cloud
              return jsonify(json_list=clouds_d)
      return None
  </code></pre>
  
  <p>Ignore my previous comment, that allowed the request to work but all the objects returned were undefined.</p>
  
  <p>This is how I did it in the end:</p>
  
  <pre><code>clouds_d = dict( (cloud.name, [ photo.to_dict() for photo in cloud.photos ]) for cloud in clouds )
  return jsonify(clouds_d)
  </code></pre>