title: Pymongo: Cannot encode object of type decimal.Decimal?
body: <p>After trying to <a href="https://api.mongodb.com/python/current/tutorial.html" rel="nofollow noreferrer">insert_one</a> into a collection. 
  I recieve this error:</p>
  
  <pre><code>bson.errors.InvalidDocument: cannot encode object: Decimal('0.16020'), of type: &lt;class 'decimal.Decimal'&gt;
  </code></pre>
  
  <p>The code runs fine when the JSON does not include the <code>decimal.Decimal</code> object. If there is a solution can you kindly consider coding it in a recursive manner to made the whole of the python dictionary <code>json_dic</code> compatible to be inserted into MongoDB (as there is more than once instance of the class decimal.Decimal in the <code>json.dic</code> entries). </p>
  
  <p>Any help would be appreciated!</p>
  
  <p><strong>EDIT 1:</strong> Here is the json I am dealing with </p>
  
  <pre><code>import simplejson as json
  from pymongo import MongoClient
  
  json_string = '{"A" : {"B" : [{"C" : {"Horz" : 0.181665435,"Vert" : 0.178799435}}]}}'
  
  json_dict = json.loads(json_string)
  this_collection.insert_one(json_dict)
  </code></pre>
  
  <p>This produces 
  <code>bson.errors.InvalidDocument: cannot encode object: Decimal('0.181665435'), of type: &lt;class 'decimal.Decimal'&gt;</code></p>
  
  <p><strong>EDIT 2:</strong> Unfortunately my example above simplified my existing JSON too much and the answer provided by @<a href="https://stackoverflow.com/a/61461289/157416">Belly Buster</a> (despite working fine with the Json above) thows an error: </p>
  
  <p><code>AttributeError: 'decimal.Decimal' object has no attribute 'items'</code></p>
  
  <p>with my actual Json, so I am providing the full Json here hopefully to find out what is wrong (also <a href="https://i.imgur.com/eevUIyV.png" rel="nofollow noreferrer">as a screen-shot</a>):</p>
  
  <pre><code>json_string = 
  '
  {
    "Setting" : {
      "GridOptions" : {
        "Student" : "HighSchool",
        "Lesson" : 1,
        "Attended" : true
      },
      "Grades" : [
        80,
        50.75
      ],
      "Count" : 2,
      "Check" : "Coursework",
      "Passed" : true
    },
    "Slides" : [
      {
        "Type" : "ABC",
        "Duration" : 1.5
      },
      {
        "Type" : "DEF",
        "Duration" : 0.5
      }
    ],
    "Work" : {
      "Class" : [
        {
          "Time" : 123456789,
          "Marks" : {
            "A" : 50,
            "B" : 100
          }
        }
      ],
      "CourseWorkDetail" : [
        {
          "Test" : {
            "Mark" : 0.987654321
          },
          "ReadingDate" : "Feb162006",
          "Reading" : 300.001,
          "Values" : [
            [
              0.98765
            ],
            [
              -0.98765
            ]
          ]
        },
        {
          "Test" : {
            "Mark" : 0.123456789
          },
          "ReadingDate" : "Jan052010",
          "Reading" : 200.005,
          "Values" : [
            [
              0.12345
            ],
            [
              -0.12345
            ]
          ]
        }
      ]
    },
    "listing" : 5
  }
  '
  </code></pre>
  
  <p><strong>Edit 3:</strong>
  Complimentory to the answer below, you can iterate recursively in a dictionary like this and use the function from the answer</p>
  
  <pre><code>def iterdict(dict_items, debug_out):
      for k, v in dict_items.items():
          if isinstance(v):
              iterdict(v)
          else:
              dict_items[k] = convert_decimal(v)
      return dict_items
  </code></pre>