json_util – инструмент для использования Python json модуля с BSON документами

Данный модуль предоставляет два вспомогательных метода для преобразования json в BSON объекты.

from bson import Binary, Code
from bson.json_util import dumps

dumps([{'foo': [1, 2]},
       {'bar': {'hello': 'world'}},
       {'code': Code("function x() { return 1; }")},
       {'bin': Binary("")}])
# '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }", "$scope": {}}}, {"bin": {"$binary": "AQIDBA==", "$type": "00"}}]'
from bson.json_util import loads
loads('[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": "00", "$binary": "AQIDBA=="}}]')
# [{u'foo': [1, 2]}, {u'bar': {u'hello': u'world'}}, {u'code': Code('function x() { return 1; }', {})}, {u'bin': Binary('...', 0)}]
bson.json_util.default(obj)
bson.json_util.dumps(obj, *args, **kwargs)

Враппер над json.dumps.

bson.json_util.loads(s, *args, **kwargs)

Враппер json.loads.

bson.json_util.object_hook(dct)