0

I Need Help , I wanna add data From Json File to MongoDb , i use this code but it give me an error : "FormatException : Cannot deserialize a 'BsonDocument' from BsonType 'Array'." :( :( this is my code

static async Task MainAsync()
    {
        var connectionString = "mongodb://localhost:27017";
        
         var client = new MongoClient(connectionString);
         IMongoDatabase database = client.GetDatabase("test");
        
         string json = File.ReadAllText("D:\\Test.json");
         //MessageBox.Show(json);
        var document = new BsonDocument();
        BsonDocument doc = BsonDocument.Parse(json);
        document.Add(BsonDocument.Parse(json));
        BsonSerializer.Deserialize<BsonDocument>(json);
        //BsonDocument document = BsonDocument.Parse(json.ToString());
         var collection = database.GetCollection<BsonDocument>("test_collection");
         await collection.InsertOneAsync(document);

And this is th code that i use it to create the JSON file :

public bool WriteJason(DataTable dt, string path)
    {
        try
        {

            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>();
            Dictionary<string, string> row = null;

            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, string>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName.Trim().ToString(), Convert.ToString(dr[col]));
                }
                rows.Add(row);
            }
            string jsonstring = serializer.Serialize(rows);

            using (var file = new StreamWriter(path, false))
            {
                file.Write(jsonstring);
                file.Close();
                file.Dispose();
            }
            return true;
        }
        catch { return false; }
    }
6
  • Code in question, please. It's surprisingly hard to debug an image. The compiler cannae do it cap'n. Also you ought to provide an example of your JSON (also in your question). Click the edit button to edit your question. Commented Oct 5, 2018 at 0:22
  • Thx for the advice Commented Oct 5, 2018 at 0:39
  • Can you also include some sample JSON? Commented Oct 5, 2018 at 0:39
  • now i doing the test with simple json file , evrey time i change the file because evrey time the director give me a file to work with it so we dont have a fixed form of file , i didn't know if i well explain it or not Commented Oct 5, 2018 at 0:45
  • What is document.Add(BsonDocument.Parse(json)); for? Commented Oct 5, 2018 at 0:54

1 Answer 1

2

You need a lot less code to do that, this should be enough:

 string json = File.ReadAllText("D:\\Test.json");
 BsonDocument doc = BsonDocument.Parse(json);
 var collection = new MongoClient("mongodb://localhost:27017").GetDatabase("test").GetCollection<BsonDocument>("test_collection");
 await collection.InsertOneAsync(doc);

If that doesn't work then something is wrong with your JSON file which you would need to post here.

Sign up to request clarification or add additional context in comments.

3 Comments

i add the code that i use it to create the json file , can you take a look on it
Ok but what is actually in the file?
actually the file content Information of a company ( business intelligence ) that's why i'm doing test with simple file content information like Id , Name , Address ..., i think the prb is in the method that create the json file it's give me a json file with one line ( all the information in one line )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.