Skip to main content

BSON Format

Description

BSON (Binary JSON) is a binary-encoded serialization of JSON-like documents. It's used by MongoDB for data storage and network transfer. BSON extends JSON with additional data types like Date and Binary, and stores data more efficiently than JSON.

File Extensions

  • .bson - BSON files

Implementation Details

Reading

The BSON implementation:

  • Uses bson library for decoding
  • Reads BSON documents sequentially from file
  • Converts BSON documents to Python dictionaries
  • Handles BSON-specific types (Date, Binary, etc.)

Writing

Writing support:

  • Encodes Python dictionaries to BSON
  • Writes BSON documents sequentially
  • Handles BSON-specific types

Key Features

  • Binary format: More efficient than JSON
  • Extended types: Supports Date, Binary, and other types
  • Nested data: Supports complex nested structures
  • MongoDB compatible: Works with MongoDB data

Usage

from iterable import open_iterable

# Basic reading
source = open_iterable('data.bson')
for row in source:
print(row)
source.close()

# Writing
dest = open_iterable('output.bson', mode='w')
dest.write({'name': 'John', 'age': 30})
dest.close()

Parameters

No specific parameters required.

Limitations

  1. bson dependency: Requires bson package
  2. Binary format: Not human-readable
  3. Type conversion: Some BSON types may be converted to Python equivalents
  4. File format: Expects sequential BSON documents (not MongoDB dump format)

Compression Support

BSON files can be compressed with all supported codecs:

  • GZip (.bson.gz)
  • BZip2 (.bson.bz2)
  • LZMA (.bson.xz)
  • LZ4 (.bson.lz4)
  • ZIP (.bson.zip)
  • Brotli (.bson.br)
  • ZStandard (.bson.zst)

Use Cases

  • MongoDB data: Exporting/importing MongoDB collections
  • Binary JSON: When JSON is too verbose
  • Data transfer: Efficient binary data exchange
  • Type preservation: When you need to preserve specific data types
  • JSON - Text-based JSON format
  • JSONL - Line-delimited JSON
  • MessagePack - Another binary JSON format