0

my code here:

import os

import bson
from bson.codec_options import CodecOptions

bson_codec_options = CodecOptions(
    datetime_conversion='DATETIME_AUTO',
    tz_aware=True,
    unicode_decode_error_handler='ignore'
)
if __name__ == '__main__':
    current_dir = os.getcwd()
    bson_files = []

    for file in os.listdir(current_dir):
        current_item = os.path.join(current_dir, file)
        if os.path.isfile(current_item) and file.endswith(".bson"):
            bson_files.append(file)

    for file in bson_files:
        
        with open(os.path.join(current_dir, file), 'rb') as fcache:
            data = bson.decode(fcache.read(), codec_options=bson_codec_options)
            print(data)

I try to convert BSON to JSON. But I get error:

File "C:\PyProjects\zulip\.venv\Lib\site-packages\bson\__init__.py", line 1094, in decode
return cast("Union[dict[str, Any], _DocumentType]", _bson_to_dict(data, opts))
                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object cannot be interpreted as an integer

I hope anyone had already encountered such error, what did you do?

5
  • This normally happens when the BSON decoder encounters a string where it expects an integer. Since you have not shown us your code for _bson_to_dict(), it is hard to say. Commented Nov 26, 2024 at 15:02
  • 1. Are you sure those files are actually BSON files and not just from the file extension? 2. Provide an example of one of those .bson files where the decode is failing. Commented Nov 26, 2024 at 15:15
  • drive.google.com/file/d/1adzMEa1ckRNK_3RpbAcuuZfeqx-ghMLw/… @aneroid Hi, here an example. Naveed Ahmed, _bson_to_dict() It is the function from library pymongo==4.10.1. To tell the truth debugger doesn't reach this function. Commented Nov 27, 2024 at 6:41
  • @Griffith No, don't post links to google drive. Edit your question and add the bson sample there. Commented Nov 27, 2024 at 7:20
  • @aneroid if I could I would do it. Unfortunately stackoveflow doesn't allow to upload files. I don't think u want to get raw(text) content from file. Commented Nov 27, 2024 at 9:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.