title: convert pymongo cursor to json
body: <ul>
  <li>I know this is a fairly common problem. I'm writing a small Flask app and I'm trying to feed some queries back to the view.
  I've connected to my local MongoDB setup, and made a successful query - but I can't generate a json object with it. </li>
  </ul>
  
  <p>The most common solution I've seen is to import json_util from pymongo i.e. </p>
  
  <pre><code>import json
  from pymongo import json_util
  
  results = connection.get_collection('papayas_papaya')
  results = results.find({
      'identifier': '1',
  })
  serialized_results = [json.dumps(result, default=json_util.default, separators=(',', ':')) for result in results]
  </code></pre>
  
  <p>I've installed pymongo into my Flask virtualenv using pip i.e :</p>
  
  <pre><code>pip install pymongo
  </code></pre>
  
  <p>When running the above code I keep getting the following error:</p>
  
  <pre><code>ImportError: cannot import name json_util
  </code></pre>
  
  <p>I can see this line in the pymongo-2.3-py2.6.egg-info/installed-files.txt
      ../bson/json_util.py</p>
  
  <p>Anyone got any tips that can help me figure out what I'm doing wrong?</p>
  
  <p><strong>UPDATE</strong>:
  Having noodled about with this a little further - I've managed to get this working thus:</p>
  
  <pre><code>import pymongo
  from bson.json_util import dumps
  
  connection = pymongo.Connection("localhost", 27017)
  db = connection.mydocs
  
  def get():
      cursor = db.foo.find({"name" : "bar"})
      return dumps(cursor)
  </code></pre>
  
  <p>One of the problems I had was trying to pip install bson independently - pymongo brings bson with it and importing bson separately caused problems.</p>
  
  <p>Thanks @Cagex for pointing me in the right direction</p>