0

So I'm trying to save my block into mongodb but I'm getting this error I've already tried all the answers on google/stackoverflow/some other sites but I'm not getting the same results as everyone else. Am I missing something?

import json
from bson import json_util

client = pymongo.MongoClient("****")

blockData = json.dumps(p1.get_latest_block().__dict__, default=json_util.default)
print(blockData)

db = client.blockchain
block = db.blocks

block.insert_one(blockData)

This is the block data

{
  "_id":1,
  "index_no":1,
  "data":"John",
    "timestamp":{
      "$date":1593037993478
    },
  "previous_hash":0,
  "hash":"090c384953d601ecda0d12f4cade9f2fb81029739b4642db900b75e56d7f55b6",
  "nonce":37
}
Nik3348
  • 23
  • 2
  • 6
  • Why are you calling json.dumps? – D. SM Jun 24 '20 at 15:41
  • I was following this thread https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable – Nik3348 Jun 24 '20 at 16:17
  • That is for serialization to extended json. When you insert data no serialization is needed (nor is it correct to attempt it). pymongo has usage examples in its documentation, if you haven't read those I suggest you start there. – D. SM Jun 24 '20 at 18:18
  • Its cause block is an object, so I'm trying to change it to json – Nik3348 Jun 24 '20 at 18:40
  • If you want to store an object, store the object as it is. – D. SM Jun 24 '20 at 18:41

0 Answers0