JSON

Libbson provides routines for converting to and from the JSON format. In particular, it supports the MongoDB extended JSON format.

Converting BSON to JSON

There are often times where you might want to convert a BSON document to JSON. It is convenient for debugging as well as an interchange format. To help with this, Libbson contains the function bson_as_json().

Convert to JSON { "a" : 1 }
Converting JSON to BSON

Converting back from JSON is also useful and common enough that we added bson_init_from_json() and bson_new_from_json().

The following example creates a new bson_t from the JSON string {"a":1}.

Convert from JSON
Streaming JSON Parsing

Libbson provides bson_json_reader_t to allow for parsing a sequence of JSON documents into BSON. The interface is similar to bson_reader_t but expects the input to be in the MongoDB extended JSON format.

<file>json-to-bson.c</file>
Examples

The following example reads BSON documents from stdin and prints them to stdout as JSON.

<file>bson-to-json.c</file>