Resolved get value multiple map inside list

riya

New member
Joined
Jan 21, 2023
Messages
2
Programming Experience
Beginner
Hi guys,

I am very new to c#, i want to fetch data from below structure. Can someone please help me how can i fetch same.

JSON:
"replicableInsuranceDetails" : [
    {
        "insurance" : "new",
        "insuredGender" : "M",
        "insuredEmploymentStatus" : "empPartTime",
        "insuranceName" : "test",
        "policyOrMemberId" : "policy-123",
        "insuredName" : "kdkas",
        "insuredRelationship" : "nephewOrNiece"
    },
    {
        "insurance" : "Insurance number Two",
        "insuredGender" : "M",
        "insuredEmploymentStatus" : "selfEmployed",
        "insuranceName" : "test2",
        "policyOrMemberId" : "POLICY Two",
        "insuredName" : "Two  Jonny",
        "insuredRelationship" : "spouseee"
    },
    {
        "insurance" : "selfPay",
        "insuredGender" : "M",
        "insuredEmploymentStatus" : "Unknown",
        "insuranceName" : "test",
        "policyOrMemberId" : "Unavailable",
        "insuredName" : "Unavailable",
        "insuredRelationship" : "employee"
    }
]
 
Last edited by a moderator:
That is JSON data. What have you tried and where are you stuck?
Hi @jmcilhinney

i have tried this code
C#:
var replicableInsuranceDetails = value.GetValue("replicableInsuranceDetails").AsBsonArray;

Console.WriteLine( "replicableInsuranceDetails :" +replicableInsuranceDetails.GetType());
Console.WriteLine("replicableInsuranceDetails.Count "+replicableInsuranceDetails.Count);

for(var i = 0; i < replicableInsuranceDetails.Count; i++) {
    var insure = replicableInsuranceDetails.ToBsonDocument();

    Console.WriteLine("insure "+insure);

    if (insure.Contains("insuranceName")) {
        insuranceName = insure.GetValue("insuranceName").ToString();
        Console.WriteLine( "InsuranceName :" +insuranceName);
    }

    var insure1 = replicableInsuranceDetails.ToBsonDocument();

    if (insure.Contains("insuranceName")) {
        SecondaryinsuranceName = insure.GetValue("insuranceName").ToString();
        Console.WriteLine( "InsuranceName :" +insuranceName);
    }
}
But by using this everytime i am getting value of 3rd map only.
 
Last edited by a moderator:
I and not familiar with how those presumably MongoDB ToBson*() methods work.

What is the output of lines 2 and 3?

Did line 9 print out 3 times?

As an aside, why would you expect lines 18-21 to behave differently than lines 11-14 if you are getting the same data from the same source?
 
Oof, that looks like hard work. I'd head over to quicktype and dump that json into it (you'll need to fix it first, it's not valid to have an atttibute outside an object) and get the class it makes, read the comment closely, and have a code like:

C#:
var replicableInsuranceDetails = YourChosenRootName.FromJson(thatString).ReplicableInsuranceDetails;

foreach(var rid in replicableInsuranceDetails)
  Console.Write(rid.InsuranceName);
 

Latest posts

Back
Top Bottom