- Info:
See github for the latest source.
About
A fast BSON to MongoDB Extended JSON converter for Python that uses libbson.
Installation
python-bsonjs can be installed with pip:
$ python -m pip install python-bsonjs
Examples
>>> import bsonjs
>>> bson_bytes = bsonjs.loads('{"hello": "world"}')
>>> bson_bytes
'\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00'
>>> bsonjs.dumps(bson_bytes)
'{ "hello" : "world" }'
Using bsonjs with pymongo to insert a RawBSONDocument.
>>> import bsonjs
>>> from pymongo import MongoClient
>>> from bson.raw_bson import RawBSONDocument
>>> client = MongoClient("localhost", 27017, document_class=RawBSONDocument)
>>> db = client.test
>>> bson_bytes = bsonjs.loads('{"_id": 1, "x": 2}')
>>> bson_bytes
'\x15\x00\x00\x00\x10_id\x00\x01\x00\x00\x00\x10x\x00\x02\x00\x00\x00\x00'
>>> result = db.test.insert_one(RawBSONDocument(bson_bytes))
>>> result.inserted_id # NOTE: inserted_id is None
>>> result.acknowledged
True
>>> raw_doc = db.test.find_one({'x': 2})
>>> raw_doc.raw == bson_bytes
True
>>> bsonjs.dumps(raw_doc.raw)
'{ "_id" : 1, "x" : 2 }'
Speed
bsonjs is roughly 3-4x faster than PyMongo’s json_util at decoding BSON to JSON and encoding JSON to BSON. See benchmark.py:
$ python benchmark.py Timing: bsonjs.dumps(b) 10000 loops, best of 3: 0.04682216700166464 Timing: json_util.dumps(bson.decode(b)) 10000 loops, best of 3: 0.17319270805455744 bsonjs is 3.70x faster than json_util Timing: bsonjs.loads(j) 10000 loops, best of 3: 0.053156834095716476 Timing: bson.encode(json_util.loads(j)) 10000 loops, best of 3: 0.15982166700996459 bsonjs is 3.01x faster than json_util
Limitations
Top Level Arrays
Because libbson does not distinguish between top level arrays and top level documents, neither does python-bsonjs. This means that if you give dumps or dump a top level array it will give you back a dictionary. Below are two examples of this behavior
>>> import bson
>>> from bson import json_util
>>> import bsonjs
>>> bson.decode(bsonjs.loads(json_util.dumps(["a", "b", "c"])))
{'0': 'a', '1': 'b', '2': 'c'}
>>> bson.decode(bsonjs.loads(json_util.dumps([])))
{}
One potential solution to this problem is to wrap your list in a dictionary, like so
>>> list = ["a", "b", "c"]
>>> dict = {"data": list}
>>> wrapped = bson.decode(bsonjs.loads(json_util.dumps(dict)))
{'data': ['a', 'b', 'c']}
>>> wrapped["data"]
['a', 'b', 'c']
Installing From Source
python-bsonjs supports CPython 3.9+.
Compiler
You must build python-bsonjs separately for each version of Python. On Windows this means you must use the same C compiler your Python version was built with.
Windows build requires Microsoft Visual Studio 2015
Source
You can download the source using git:
$ git clone https://github.com/mongodb-labs/python-bsonjs.git
Install
Once you have the source properly downloaded, build and install the package:
$ pip install -v .
Test
To run the test suite:
$ python -m pytest
Release files for python-bsonjs 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_bsonjs-0.7.0.tar.gz | 185.9 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| python_bsonjs-0.7.0-cp39-abi3-win_amd64.whl | CPython 3.9 | abi3 | Windows x86-64 | Details |
| python_bsonjs-0.7.0-cp39-abi3-win32.whl | CPython 3.9 | abi3 | Windows x86-32 | Details |
| python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_x86_64.whl | CPython 3.9 | abi3 | Linux musl 1.2+ x86-64 | Details |
| python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_aarch64.whl | CPython 3.9 | abi3 | Linux musl 1.2+ ARM64 | Details |
| python_bsonjs-0.7.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl | CPython 3.9 | abi3 | Linux glibc 2.17+ x86-64, Linux glibc 2.28+ x86-64 | Details |
| python_bsonjs-0.7.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl | CPython 3.9 | abi3 | Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 | Details |
| python_bsonjs-0.7.0-cp39-abi3-macosx_11_0_arm64.whl | CPython 3.9 | abi3 | macOS 11.0+ ARM64 | Details |
Total release size: 1.9 MB
Release files / python_bsonjs-0.7.0.tar.gz
| Download URL | python_bsonjs-0.7.0.tar.gz |
|---|---|
| Size | 185.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ff8f3f0b48177b07b7dc082314352745db04b4a77c2f27e2b368838ed75c9099
|
|
BLAKE2b-256 checksum How to use checksums |
43acb8bd7830ce0fb25900b70231e53b418429d7d1928899fb22594426d88035
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.
Transparency logRelease files / python_bsonjs-0.7.0-cp39-abi3-win_amd64.whl
| Download URL | python_bsonjs-0.7.0-cp39-abi3-win_amd64.whl |
|---|---|
| Size | 117.4 kB |
| Tags | CPython 3.9 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
c6dc02c2f3306fd296722abbf6f3097fc746ae4372a9177b720738a4c3df2833
|
|
BLAKE2b-256 checksum How to use checksums |
b3cffc5e59da6ed5a9c9bd3710040f89cf868f66348d3f89dbc1e60cecf4198f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.
Transparency logRelease files / python_bsonjs-0.7.0-cp39-abi3-win32.whl
| Download URL | python_bsonjs-0.7.0-cp39-abi3-win32.whl |
|---|---|
| Size | 103.8 kB |
| Tags | CPython 3.9 Windows x86-32 abi3 |
|
SHA-256 checksum How to use checksums |
8999289d7aa4c3101e5557f177aeb3ed44ddefca3aa91d5351c8736f4b29eccd
|
|
BLAKE2b-256 checksum How to use checksums |
e1f6ef6ae3f3d16eb096a2aa55570e76127f4bc83c4c43ef37673647dc749517
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.
Transparency logRelease files / python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_x86_64.whl
| Download URL | python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 342.0 kB |
| Tags | CPython 3.9 Linux musl 1.2+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
d199ba86d947763b7df876a6bb51938774a857f720581f254e971c675f6fb2b8
|
|
BLAKE2b-256 checksum How to use checksums |
f49ea22a51a0ba90d2f05de3646124bd440eb9f9498f93c62f5a0030a8c4ea17
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.
Transparency logRelease files / python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_aarch64.whl
| Download URL | python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 353.0 kB |
| Tags | CPython 3.9 Linux musl 1.2+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
5bddca761133c0d9e331177e8586f8e20b10075774a575387b8d338d14f0a956
|
|
BLAKE2b-256 checksum How to use checksums |
19fbc743c34014fc299bd402fed71945cbf2ee68aeb53148320d6a56d40d2dc1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.
Transparency logRelease files / python_bsonjs-0.7.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
| Download URL | python_bsonjs-0.7.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 349.1 kB |
| Tags | CPython 3.9 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
5057948d12a5d11ffff26048e1d24b8bee4a396e1acb5a6c57ed534e3a738aec
|
|
BLAKE2b-256 checksum How to use checksums |
fa6aaac1b495d522c939a6ef96f6ff4db99c2a8486b8760482760d7bbad93062
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.
Transparency logRelease files / python_bsonjs-0.7.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
| Download URL | python_bsonjs-0.7.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 360.1 kB |
| Tags | CPython 3.9 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
b2bd723f6d06fdf73e8d527387f431928bdcbb38cf950c96909141d4270818c6
|
|
BLAKE2b-256 checksum How to use checksums |
b018a5096f2ea56072e97e145577c242dd60f27c250ba149e4733b6291fbcfc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.
Transparency logRelease files / python_bsonjs-0.7.0-cp39-abi3-macosx_11_0_arm64.whl
| Download URL | python_bsonjs-0.7.0-cp39-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 108.1 kB |
| Tags | CPython 3.9 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
c74da3cce55ab78a02a4c75b4a8203c881d73f1b8fb47538fb562bba18a11f5d
|
|
BLAKE2b-256 checksum How to use checksums |
4e542b291b8b9d7ff4718b41404e048716999dce2bbbe6df8f301edf336777cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.
Transparency log