1

I'm trying to convert a string into a BsonDocument with Parse() method, but I keep getting the error "Cannot deserialize a 'BsonDocument' from BsonType 'String'.". My string is in the following format: string str = {'name': 'John', 'surname':'McLure'} and I call the method this way BsonDocument document = BsonDoucment.Parse(str). I saw Convert string into MongoDB BsonDocument and it says to use Parse method to convert a string into a BsonDocument, but it isn't working!

Thanks in advance

Andrea Cristiani
  • 351
  • 2
  • 5
  • 13

1 Answers1

2

Oh God, I solved this: the problem was with the format of the JSon string, I had

string json_string = {'name': 'John', 'surname':'McLure'}

, but BsonDocument.Parse() doesn't want fields in quotes, just the field name, so I changed it to

string json_string = {name: 'John', surname:'McLure'}.

Andrea Cristiani
  • 351
  • 2
  • 5
  • 13
  • 1
    But C# does want its whole strings inside `""` – bommelding Sep 27 '18 at 11:44
  • IM working with an object serialized to JSON, and in C Charp it has a ton of quotes and backslashes ```"\"{\\\"Zots\\\": \\\"zots are a unit of measure\\\", \\\"Krambles\\\": 12}\""``` -- surely there is a better way to parse from this thing? – Vincent Buscarello Oct 09 '18 at 18:20