2

I am writing a C# application that reads from an XML file, converts that to JSON, and uploads to MongoDB. Some of our tags are structured with a period at the end, like so:

<BatteryTest.>GOOD</BatteryTest.>

Using the Newtonsoft library I am able to convert the XML to JSON without a problem. It is when I go to deserialize it to a BsonDocument that I have trouble:

var document = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonText);

I get the following error message:

An exception of type 'MongoDB.Bson.BsonSerializationException' occurred in mscorlib.dll but was not handled in user code

Additional information: Element name 'BatteryTest.' is not valid'.

I have looked at the documentation but I haven't found anything that would explain how I can change the formatting properties of the deserializer. This is valid XML so I am not sure why the deserializer would choke on it, either.

Is this invalid JSON? If so, is there a way to still insert it into MongoDB without dropping that period?

Community
  • 1
  • 1
AdamMc331
  • 16,492
  • 10
  • 71
  • 133

1 Answers1

2

As the the dot can be used in MongoDB queries, it can not be used in field names. You will have to preprocess the JSON before converting it into a BSONDocument.

Alex
  • 21,273
  • 10
  • 61
  • 73
  • 1
    Dang. Not the answer I was hoping for, but it was the right one. Thank you! I will go talk to the dev team to see what we can do about this. – AdamMc331 Dec 22 '15 at 16:47
  • 1
    This is an unbelievable design flaw in Mongo. WTF? Seriously? How did they manage to confuse things on these levels? Any valid JSON should be possible to be saved into the DB. Now I need to change my program to accommodate the Mongo quirks... – PeterD Jun 13 '17 at 21:21