json_util – Tools for using Python’s json module with MongoDB documents

New in version 1.1.1.

Tools for using Python’s json module with MongoDB documents.

This module provides two methods: object_hook and default. These names are pretty terrible, but match the names used in Python’s json library. They allow for specialized encoding and decoding of MongoDB documents into Mongo Extended JSON‘s Strict mode. This lets you encode / decode MongoDB documents to JSON even when they use special PyMongo types.

Example usage (serialization):

>>> json.dumps(..., default=json_util.default)

Example usage (deserialization):

>>> json.loads(..., object_hook=json_util.object_hook)

Currently this does not handle special encoding and decoding for Binary and Code instances.

Changed in version 1.2: Added support for encoding/decoding datetimes and regular expressions.

pymongo.json_util.default(obj)
pymongo.json_util.object_hook(dct)

Previous topic

son_manipulator – Manipulators that can edit SON documents as they are saved or retrieved

Next topic

cursor_manager – Managers to handle when cursors are killed after being closed

This Page