Convert BSON to valid JSON

11,645

You can try something like this

var document = new BsonDocument("_id", ObjectId.GenerateNewId());
    var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; // key part
    Console.WriteLine(document.ToJson(jsonWriterSettings));

For More info https://groups.google.com/forum/#!topic/mongodb-user/fQc9EvsPc4k

Share:
11,645

Related videos on Youtube

BanksySan
Author by

BanksySan

system.out.println("Hello World");

Updated on September 16, 2022

Comments

  • BanksySan
    BanksySan over 1 year

    The BsonDocument.ToJson() method returns invalid JSON, as ObjectID() and ISODate are not valid JSON.

    What's the best way to get valid JSON from an arbitary BSON document?

    • Blakes Seven
      Blakes Seven about 8 years
      I find it odd that "strict" mode is not the default, but that is the case. At a guess I'd say more users voted for being able to "cut/paste" output directly into the MongoDB shell.