Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:25:30+00:00 2026-05-27T03:25:30+00:00

I am using MongoDB to bring back a record and I have the Json

  • 0

I am using MongoDB to bring back a record and I have the Json correctly returned and I was planning on converting the Json into a Dictionary of the format Dictionary but whenever I run the following method it returns an empty Dictionary (Caused by an exception being caught).

 1.   public Dictionary<string, object> convertJsonToDict(string json)
 2.   { 
 3.       try
 4.       {
 5.           Dictionary<string, object> dict = new Dictionary<string,object>();
 6.           dict = (Dictionary<string, object>)BsonSerializer.Deserialize(BsonSerializer.Deserialize<QueryDocument>(json), typeof(Dictionary<string, object>));
 7.           return dict;
 8.       }
 9.       catch
10.        {
11.            return new  Dictionary<string, object>();
12.        }
13.    }

When debugging I found the exception to be on line 6 when it tries to Deserialize the Json and the error is

FileFormatException: A document being deserialized to System.Object must be empty.

The Json String being passed in is

  "{ 
  \"_id\" : ObjectId(\"4ed1062129d8145d74e9ab2f\"), 
  \"1\" : \"New Guy\", 
  \"2\" : \"26/11/2011\", 
  \"4_25\" : \"Yes\", 
  \"4_26\" : \"No\", 
  \"5_25\" : \"No\", 
  \"5_26\" : \"Yes\", 
  \"6_25\" : \"Yes\", 
  \"6_26\" : \"No\", 
  \"7_25\" : \"\", 
  \"7_26\" : \"Comment in second box\", 
  \"19_21\" : { \"verified\" : true }, 
  \"10_19\" : \"0%\", 
  \"10_20\" : \"75% +\", 
  \"11_19\" : \"Diffuse\", 
  \"11_20\" : \"Localised\", 
  \"12_19\" : \"0-25%\", 
  \"12_20\" : \"50-75%\", 
  \"13_19\" : \"25-50%\", 
  \"13_20\" : \"25-50%\", 
  \"14_19\" : \"50-75%\", 
  \"14_20\" : \"0-25%\", 
  \"15_19\" : \"75% +\", 
  \"15_20\" : \"0%\", 
  \"17\" : \"Some comments at the bottom\" 
  }"

What is going wrong? I suspect it is something to do with the fact that there is an object at key “19_21” but this shouldn’t cause it to break, should it?

Note the line breaks are just to make the Json easier to read and are not passed in. Also I did not write this code so I don’t know why BsonSerializer is being used here, of if there is a better option that I can use.

Edit: Thank you to the people who have responded, the double serialization was due to the value in *19_21* originally was stored as a Json string inside a document, it is now a document within a document so it’s type was string before so this used to work, And correctly people have noted that I only need to deserialize once using

 dict = (Dictionary<string, object>)BsonSerializer.Deserialize(json, typeof(Dictionary<string, object>);

Thank you @L.B

This unfortunately still doesn’t solve my problem though, it will work if the object is a string in *19_21* but will still get a File format error if I use the new format of Document within document, does anybody have any ideas of what I need to change to make this work correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:25:31+00:00Added an answer on May 27, 2026 at 3:25 am

    You are correct in your suspicion that it has to do with the embedded document. The problem is that even though your Dictionary is storing the values as type object, the deserializer has to pick some concrete subclass of object to actually instantiate, and when it gets to the { verified : true } embedded document it can’t figure out what data type it should deserialize that to (if the embedded document were empty it would choose object, but it can’t choose object when the document has values that need to be deserialized).

    You could deserialize to a BsonDocument which is very similar to a Dictionary just that the values are of type BsonValue instead of object. In this case the embedded document would just be another instance of BsonDocument.

    Another response also commented on the double calls to Deserialize. There should only be one call needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have been using MongoDB as our main datastore for a little while but
I'm currently using MongoDB to record application logs, and while I'm quite happy with
I'm building a Rails application using MongoDB as the back-end and MongoMapper as the
I'm using MongoDB in a reporting system and have to delete a whole bunch
I have inserted and fetched data using MongoDB, in PHP. Is there an actual
Using mongodb with the NoRM driver I have this document: { _id : ObjectId(0758030341b870c019591900),
Im using mongodb + java. I tried to insert a new document into an
I'm considering using mongodb on a new project, but before delving in, I'd like
I have been using mongodb for a while now...been using the NoRM drivers pretty
I am started using MongoDB few days ago, and i have problem in understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.