title: JSON serializing Mongodb
body: <p>I am using the python package pymongo to retrieve data from a mongodb database.</p>
  
  <pre><code>&gt;&gt;&gt; r = collection.find()   # returns an object of class 'Cursor'
  </code></pre>
  
  <p>Then I convert to a list</p>
  
  <pre><code>&gt;&gt;&gt; l = list(r)             # returns a 'list' of 'dict'
  </code></pre>
  
  <p>here is what print(l) returns:</p>
  
  <pre><code>&gt;&gt;&gt; [{u'date': datetime.datetime(2009, 11, 10, 10, 45), u'_id': 1, u'name': u'name1', u'value': 11},{u'date': datetime.datetime(2013, 11, 10, 10, 45), u'_id': 2, u'name': u'name2', u'value': 22}]
  </code></pre>
  
  <p>Now I need to convert to JSON so that I can manipulate it.   </p>
  
  <pre><code>&gt;&gt;&gt; json.dumps(l)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
      return _default_encoder.encode(obj)
    File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
      chunks = self.iterencode(o, _one_shot=True)
    File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
      return _iterencode(o, 0)
    File "/usr/lib/python2.7/json/encoder.py", line 178, in default
      raise TypeError(repr(o) + " is not JSON serializable")
  TypeError: datetime.datetime(2009, 11, 12, 11, 14) is not JSON serializable
  </code></pre>
  
  <p>I have also tried to follow <em><a href="http://api.mongodb.org/python/1.7/api/pymongo/json_util.html" rel="noreferrer">http://api.mongodb.org/python/1.7/api/pymongo/json_util.html</a></em> without success:
  Edit: the recent version of the link is <a href="http://api.mongodb.org/python/current/api/bson/json_util.html" rel="noreferrer">http://api.mongodb.org/python/current/api/bson/json_util.html</a></p>
  
  <pre><code>&gt;&gt;&gt; json.dumps(l, default=json_util.default)  
  Traceback (most recent call last):  
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;  
  NameError: name 'json_util' is not defined  
  </code></pre>
  
  <p><strong>Note:</strong> precisely I need to push this result to R using the R package rPython and its function rPython::python.get("l")</p>
  
  <p><strong>Side Question</strong>: What is the u (u'Date', u'name', etc..) before each field in the list of dict?</p>