title: Insert JSON template from file into mongodb using pymongo
body: <p>I have a template of nested json in a text a file.</p>
  
  <p>The template looks like</p>
  
  <pre><code>$ cat file.txt
  { "sensor" : {
  "type":  "unknown",
  "notifications": "off"
  "timeout": "0"
  "featureset": {
    "features":  [ 
    {
    "name": "Temp",
    "enabled": "No",
    },
    {
    "name": "Humidity",
    "enabled": "No",
    } ] }
   }
  }
  </code></pre>
  
  <p>I would like, upon condition, to read the template into a python variable, then change some fields if I need to (i.e <code>"notifications": "on"</code>). Then save it into mongodb database.</p>
  
  <p>My curent code looks like:</p>
  
  <pre><code>dbclient = MongoClient()
  db = dbclient.sensors
  
  def main():
      with open ("file.txt", 'r') as template:
      data = template.read()
      print ('data: %s' % data)
      dbdata = db.testdata
      post_id = dbdata.insert_one(bson.BSON.encode(data.strip("\r\n")))
      print ('Inserted post id %s ' % post_id)
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>And this doesn't work. I get <code>TypeError: encoder expected a mapping type but got: ...</code></p>
  
  <p>How do I convert this string read from text file into a mongodb suitable format?</p>
  
  <p>Thank you</p>