Skip to content

Instantly share code, notes, and snippets.

View bhardwajkaran054's full-sized avatar

Karan Bhardwaj bhardwajkaran054

View GitHub Profile
@bhardwajkaran054
bhardwajkaran054 / JSONToOnlyPairOfValuesCsharp.cs
Created May 26, 2020 10:47
How to get only pair of field name and value from JSON file by using C#/.Net
//Newtonsoft.Json Dependency required
JObject o = new JObject
{
{ "name1", "value1" },
{ "name2", "value2" }
};
//Method-1
foreach (JProperty property in o.Properties())
{
@bhardwajkaran054
bhardwajkaran054 / ExtractDataFromBsonFileUsingCsharp.cs
Created May 26, 2020 10:39
Extract data from BSON by using ID or Field name
...
var database1 = clientDB.GetDatabase(dbName);
var collection1 = database1.GetCollection<BsonDocument>(collectionName);
//method1
using (var cursor = await collection1.Find(new BsonDocument()).ToCursorAsync())
{
while(await cursor.MoveNextAsync())
{
foreach (var doc in cursor.Current)
@bhardwajkaran054
bhardwajkaran054 / SerializationAndDeserializationToJsonUsingCSharp.cs
Last active May 26, 2020 10:29
Serialization and Deserialization to JSON using C#
//Serialization to JSON in C#
//Newtonsoft.Json library require
using Newtonsoft.Json;
namespace ....
{
class Movie
{
public int Id {get; set} //Property
@bhardwajkaran054
bhardwajkaran054 / BsonToJsonUsingCSharp.cs
Last active October 21, 2023 01:34
BSON file to JSON using C#, while working with MongoDB in C# at certain point we need to convert BSON file to JSON. But most of the developer doing same mistake for convertion in JSON, they directly try to parse the BSON file into JSON by using "...<Bsonfile>.ToJson()" and they get the same file as the output. so this is the proper way to conver…
//If you apply this code on whole BSON file(Entire Documents in single collection) then it couldn't works, so you need to take all the BSON document
//in the List and by using foreach statment you need to extract each document and then convert into JSON.
//example: ...
// var database = clientApp.GetDb(dbName);
// var collection = database.GetCollection<BsonDocument>(collectionName);
// var documents = collection.Find(new BsonDocument()).ToList();
// foreach (BsonDocument doc in documents)
// {
// var jsonWritersetting = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
// JObject json = JObject.Parse(doc.ToJson<MongoDB.Bson.BsonDocument>(jsonWritersetting));
@bhardwajkaran054
bhardwajkaran054 / ImportJsonToMongoDbUsingCsharp.cs
Created May 25, 2020 13:42
Create document in MongoDB by importing JSON file (C# / .Net)
public static void Main (string[] args)
{
var connectionString = "<Connection String>"; //example: ... = "mongodb://localhost";
var client = new MongoClient(connectionString);
var database = client.GetDatabase("<Database Name>"); //example: ...GetDatabase("test");
string text = System.IO.File.ReadAllText(@"<File Directory with FileName.json>"); //example: ...ReadAllText(@"MyMovies.json");
var document = BsonSerializer.Deserialize<BsonDocument>(text);