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
Related videos on Youtube
Comments
-
BanksySan over 1 year
The
BsonDocument.ToJson()
method returns invalid JSON, asObjectID()
andISODate
are not valid JSON.What's the best way to get valid JSON from an arbitary BSON document?
-
Blakes Seven about 8 yearsI 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.
-