BSON_INIT_FROM_JSON(3)

πŸ’… bson_init_from_json β€” the libbson glow-up where your JSON becomes a whole BSON ✨

okay so this one lives in the libbson manual, section 3, and it's literally the "turn my cute little JSON string into a real BSON object" function. bestie, it ate. let's get into it πŸŽ€


✨ SYNOPSIS ✨

bool
bson_init_from_json (bson_t *bson,
                     const char *data,
                     ssize_t len,
                     bson_error_t *error);

🌸 PARAMETERS 🌸

  • bson β€” a pointer to an uninitialized bson_t. like, don't pre-dress it, she gets initialized inside, k? 🫢
  • data β€” a UTF-8 encoded string holding valid JSON. must be valid, no cap πŸ’€
  • len β€” the length of data in bytes, excluding a trailing \0. or pass -1 and it'll figure the length out with strlen(). main character behavior honestly ⭐
  • error β€” an optional spot to stash a bson_error_t. optional optional, no pressure πŸ™ˆ

πŸ¦‹ DESCRIPTION πŸ¦‹

So bson_init_from_json() initializes a brand new bson_t by parsing the JSON it finds in data. iconic. ✨

The one rule: only a single JSON object may exist in data. more than one? then an error gets set and false comes back at you. periodt πŸ’…

Also, data should be in MongoDB Extended JSON format. that's the vibe, iykyk.


πŸ’€ DEPRECATED BEHAVIOR πŸ’€

heads up bestie, this stuff is deprecated. it works (for now) but do not build your whole personality on it.
  • If there are multiple comma-separated JSONs in data, the keys from all of them get merged into the returned BSON. fr fr. for example, {"a": 1},{"b": 2} gets parsed as {"a": 1, "b": 2}. chaos, but documented chaos πŸ“
  • If the first character encountered after the last valid JSON object is {, then all following characters are just ignored and no error is set. she simply stops reading 😭 otherwise, an error will be set and NULL returned.
  • This deprecated behavior is subject to change in a future release. so like… don't get attached? πŸ‘€

πŸ”₯ ERRORS πŸ”₯

Errors are propagated via the error parameter. that's it, that's the section πŸ’œ


πŸ‘‘ RETURNS πŸ‘‘

Returns true if valid JSON was parsed, otherwise false and error is set. πŸ“‰

On success, bson is initialized and must be freed with bson_destroy(). otherwise bson is invalid. so please, free her, she deserves peace 🌈


🧚 AUTHOR 🧚

MongoDB, Inc


2009-present, MongoDB, Inc.


okay that's the whole page, mwah 🫢 this was libbson 2.5.6, dated Oct 09, 2026 β€” so like, go forth, parse one JSON object, free your bson_t, and slay πŸ’–πŸŽ€βœ¨

πŸ“¦ where this page comes from