Search Shortcut cmd + k | ctrl + k

BSON / MongoDB Extended-JSON v2 codec in SQL — decode/validate, dotted-path field projection with coercion, ObjectId helpers, JSON round-trip

Maintainer(s): alitrack

Compatibility Matrix

This table covers all DuckDB releases that have not reached end-of-life, including LTS releases, and shows the 2 most recent patch releases of each. Older patch releases are not listed, but the extension may be available for them as well.

Platform 1.5.5 1.5.6 2.0.0 2.0.1
Linux AMD64
Linux ARM64
macOS AMD64
macOS ARM64
Windows AMD64
Windows ARM64
WebAssembly

Installing and Loading

INSTALL bson FROM community;
LOAD bson;

Example

LOAD bson;

-- {a: 1} as a BSON document
CREATE TABLE d AS SELECT from_hex('0c0000001061000100000000') AS b;

-- Extended-JSON v2 (canonical and relaxed)
SELECT bson_to_json(b, 'canonical') FROM d;   -- {"a":{"$numberInt":"1"}}
SELECT bson_to_json(b, 'relaxed')   FROM d;   -- {"a":1}

-- Validate: total, per-row, never aborts.
SELECT bson_is_valid(b) FROM d;               -- true
SELECT bson_well_formed(b) FROM d;            -- {ok: true, error: NULL, kind: NULL}

-- Field projection (dotted path) + type
SELECT bson_field(b, 'a', '') FROM d;         -- 1
SELECT bson_type_of(b, 'a')   FROM d;         -- int

-- ObjectId helpers (literal bytes, no table needed)
SELECT bson_objectid_hex(from_hex('00112233445566778899aabb'));  -- 24-char hex
SELECT to_hex(bson_objectid_from_hex('00112233445566778899aabb'));

About bson

bson — BSON / Extended-JSON v2 in SQL

Decode a BSON document to MongoDB's Extended JSON, build one back, validate it (with a classified error taxonomy), project any dotted path with a type coercion, and read/write ObjectIds — all in SQL, right where you query.

Built as a DuckDB loadable extension with the quack-rs C-API binding layer. The engine (bson_core/) is a verbatim port of the Query-farm vgi-bson bson-core crate (MIT) — a pure-Rust structural decoder over the official bson crate, with no Arrow, no VGI, and no mongodb driver; only the transport layer was replaced with direct C-API vector I/O.

9 Functions:

Function Signature Returns
bson_to_json (doc BLOB, mode VARCHAR) VARCHAR — Extended-JSON v2 (canonical / relaxed)
bson_decode (doc BLOB) VARCHAR — Extended-JSON v2 canonical
bson_from_json (json VARCHAR) BLOB — Extended-JSON v2 -> BSON bytes
bson_is_valid (doc BLOB) BOOLEAN — single well-formed BSON document
bson_well_formed (doc BLOB) STRUCT(ok, error, kind) — classified failure taxonomy
bson_field (doc BLOB, path VARCHAR, mode VARCHAR) VARCHAR — leaf at dotted path, coerced (inference / objectid / decimal / uuid / timestamp / datetime / blob / json)
bson_type_of (doc BLOB, path VARCHAR) VARCHAR — BSON type name at path (or top level)
bson_objectid_hex (oid BLOB) VARCHAR — 12-byte ObjectId -> 24-char hex
bson_objectid_from_hex (hex VARCHAR) BLOB — 24-char hex -> 12-byte ObjectId

The decoder is total and bounds nesting depth, so a hostile blob fails its own row, never crashes the scan. NULL flows through.

Added Functions

function_name function_type description comment examples
bson_decode scalar NULL NULL  
bson_field scalar NULL NULL  
bson_from_json scalar NULL NULL  
bson_is_valid scalar NULL NULL  
bson_objectid_from_hex scalar NULL NULL  
bson_objectid_hex scalar NULL NULL  
bson_to_json scalar NULL NULL  
bson_type_of scalar NULL NULL  
bson_well_formed scalar NULL NULL  

Overloaded Functions

This extension does not add any function overloads.

Added Types

This extension does not add any types.

Added Settings

name description input_type scope aliases typed_value
temp_directory Set the directory to which temporary files are written. VARCHAR GLOBAL [] post.db.tmp
© 2026 DuckDB Foundation, Amsterdam NL
DuckDB Home Code of Conduct Trademark Use Blog