title: Pymongo bson.errors.InvalidDocument: Cannot encode Object when read from file
body: <p>I have a <code>csv</code> file with a header row and 100k entries in it.</p>
  
  <p>I am using <code>python</code> in <code>pycharm</code> IDE and <code>conda</code> as python interpreter. <code>conda</code> suppies <code>pymongo-3.4.0</code> as <code>mongo-driver</code>. </p>
  
  <p>I parse my <code>csv</code> file and save it as <code>json</code>n file on disk, and then iterate over the <code>json</code> file to insert the <code>json</code> objects in the <code>mongo</code> database.</p>
  
  <p>JSON looks like this:</p>
  
  <pre><code>{
    a1 : {'name': 702104534, type='Single' }
    a2 : {'name': 702104535, type='Single' }
    a3 : {'name': 702104536, type='Married' }
  }
  </code></pre>
  
  <p>Python Code:</p>
  
  <pre><code>import pymongo
  import pickle
  
  with open('my_json_file.json', 'rb') as in_file:
      person_json = pickle.load(in_file)
  
  conn = pymongo.MonogClient('localhost','27017')
  db = conn['db']
  coll = db['coll']
  
  persons = person_json.keys()
  for person in persons:
      coll.insert_one(person_json[person])
  </code></pre>
  
  <p>Throughs the following error:</p>
  
  <pre><code>Traceback (most recent call last):
  File "C:/PyCharmWorkSpace/LCR/json2mongo.py", line 109, in &lt;module&gt;
      data2mongo()
  File "C:/PyCharmWorkSpace/LCR/json2mongo.py", line 89, in data2mongo
      coll.insert_one(json_str)
  File "C:\Anaconda3\lib\site-packages\pymongo\collection.py", line 657, in insert_one
      bypass_doc_val=bypass_document_validation),
  File "C:\Anaconda3\lib\site-packages\pymongo\collection.py", line 562, in _insert
      check_keys, manipulate, write_concern, op_id, bypass_doc_val)
  File "C:\Anaconda3\lib\site-packages\pymongo\collection.py", line 543, in _insert_one
      check_keys=check_keys)
  File "C:\Anaconda3\lib\site-packages\pymongo\pool.py", line 424, in command
      self._raise_connection_failure(error)
  File "C:\Anaconda3\lib\site-packages\pymongo\pool.py", line 552, in _raise_connection_failure
      raise error
  File "C:\Anaconda3\lib\site-packages\pymongo\pool.py", line 419, in command
      collation=collation)
  File "C:\Anaconda3\lib\site-packages\pymongo\network.py", line 94, in command
      None, codec_options, check_keys)
  bson.errors.InvalidDocument: Cannot encode object: 702104534
  </code></pre>
  
  <p>However, same works, if the json string is a variable</p>
  
  <p>Python Code:</p>
  
  <pre><code>import pymongo
  
  conn = pymongo.MonogClient('localhost','27017')
  db = conn['db']
  coll = db['coll']
  
  person = {'name': 702104534, 'type'='Single' }
  coll.insert_one(person)
  </code></pre>
  
  <p>Thanks for your help.</p>