title: TypeError: Object of type 'ObjectId' is not JSON serializable using Flask and MongoDB
body: <p>I want a dictionary to be inserted into a mongo db and to be returned.</p>
  <p>I have found a couple of questions for this Error but all are related to READING queries. Since I am just inserting and do not need to parse any output I do not understand this behavior at all.</p>
  <p>The main difference in the behavior comes with commenting in/out this line:</p>
  
  <pre><code>db_s41.links.insert_one(new_link_doc)
  </code></pre>
  
  <p>With the <strong>line commented out</strong>, the return of the object works properly. Obviously the document is not inserted.</p>
  <p>With the <strong>line not commented out</strong>, the return does not work, the error gets thrown but still but still the documents is properly inserted into the db.</p>
  <p>I am using the following code:</p>
  
  <pre><code>from flask import Flask, redirect, url_for, request, render_template
  from pymongo import MongoClient
  import os
  
  app = Flask(__name__)
  
  client = MongoClient(
      os.environ['DOCKER_FLASK_MONGODB_DB_1_PORT_27017_TCP_ADDR'],
      27017
  )
  
  # Set db object
  db_s41 = client.s41
  
  @app.route('/new_link2')
  def generate_link(berater=&quot;muster_berater&quot;, kunde='muster_kunde'):
     
      new_link_doc = {
          'link': 'test',
          'required_documents': [],
          'date_link_generated': 'test',
          'timestamp_link_generated': 'test',
          'berater_link_generated': 'test'
      }
  
      # insert into database 's41', collection 'links'
      db_s41.links.insert_one(new_link_doc)
  
      return new_link_doc
  
  if __name__ == &quot;__main__&quot;:
      app.run(host=&quot;0.0.0.0&quot;, debug=True)
   
  </code></pre>
  
  <p>I am using the following versions:</p>
  <p>Docker version 20.10.1, build 831ebea
  Mongo 4.4.2
  Python 3.6.6
  click        7.1.2<br />
  Flask        1.1.2<br />
  itsdangerous 1.1.0<br />
  Jinja2       2.11.2
  MarkupSafe   1.1.1<br />
  pip          10.0.1
  pymongo      3.11.2
  setuptools   39.2.0
  Werkzeug     1.0.1<br />
  wheel        0.31.1</p>
  <p>I am truly grateful for any ideas.</p>