#includeusing namespace bson; // create a BSON object BSONObj bsonObj = BSONObjBuilder().append("name", "John").append("age", 30).obj(); // convert the BSON object to a JSON-formatted string std::string jsonString = bsonObj.toString();
#includeIn both examples, the BSONObj::toString method is used to convert a BSON object to a JSON-formatted string. The first example just stores the result in a string variable, while the second example prints the result to the console. These examples use the BSON C++ library, which is part of the MongoDB C++ driver package.#include using namespace bson; // create a BSON object BSONObj bsonObj = BSONObjBuilder().append("name", "John").append("age", 30).obj(); // convert the BSON object to a JSON-formatted string std::string jsonString = bsonObj.toString(); // print out the JSON-formatted string std::cout << jsonString << std::endl;