Skip to main content

A library for converting between BSON and JSON.

Project description


See github for the latest source.


Shane Harvey <>


A fast BSON to MongoDB Extended JSON converter for Python.


python-bsonjs can be installed with pip:

$ python -m pip install python-bsonjs


>>> import bsonjs
>>> bson_bytes = bsonjs.loads('{"hello": "world"}')
>>> bson_bytes
>>> 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
>>> result = db.test.insert_one(RawBSONDocument(bson_bytes))
>>> result.inserted_id  # NOTE: inserted_id is None
>>> result.acknowledged
>>> raw_doc = db.test.find_one({'x': 2})
>>> raw_doc.raw == bson_bytes
>>> bsonjs.dumps(raw_doc.raw)
'{ "_id" : 1, "x" : 2 }'


bsonjs is roughly 10-15x faster than PyMongo’s json_util at decoding BSON to JSON and encoding JSON to BSON. See

$ python
Timing: bsonjs.dumps(b)
10000 loops, best of 3: 0.110911846161
Timing: json_util.dumps(bson.BSON(b).decode())
10000 loops, best of 3: 1.46571397781
bsonjs is 13.22x faster than json_util

Timing: bsonjs.loads(j)
10000 loops, best of 3: 0.0628039836884
Timing: bson.BSON().encode(json_util.loads(j))
10000 loops, best of 3: 0.683200120926
bsonjs is 11.72x faster than json_util

Installing From Source

python-bsonjs supports CPython 2.6, 2.7, and 3.3+.


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.


Note that this repository contains a git submodule. You must initialize the submodule by using:

$ git clone --recursive

Or, if your version of git does not have clone --recursive:

$ git clone
$ cd python-bsonjs
$ git submodule update --init --recursive


Once you have the source properly downloaded, build and install the package:

$ python install


To run the test suite:

$ python test

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-bsonjs-0.1.1.tar.gz (142.6 kB view hashes)

Uploaded source

Built Distributions

python_bsonjs-0.1.1-cp35-cp35m-win_amd64.whl (54.2 kB view hashes)

Uploaded cp35

python_bsonjs-0.1.1-cp35-cp35m-win32.whl (48.5 kB view hashes)

Uploaded cp35

python_bsonjs-0.1.1-cp34-cp34m-win_amd64.whl (67.7 kB view hashes)

Uploaded cp34

python_bsonjs-0.1.1-cp34-cp34m-win32.whl (61.3 kB view hashes)

Uploaded cp34

python_bsonjs-0.1.1-cp33-cp33m-win_amd64.whl (67.7 kB view hashes)

Uploaded cp33

python_bsonjs-0.1.1-cp33-cp33m-win32.whl (61.3 kB view hashes)

Uploaded cp33

python_bsonjs-0.1.1-cp27-cp27m-win_amd64.whl (67.4 kB view hashes)

Uploaded cp27

python_bsonjs-0.1.1-cp27-cp27m-win32.whl (61.0 kB view hashes)

Uploaded cp27

python_bsonjs-0.1.1-cp26-cp26m-win_amd64.whl (67.7 kB view hashes)

Uploaded cp26

python_bsonjs-0.1.1-cp26-cp26m-win32.whl (61.0 kB view hashes)

Uploaded cp26

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page