0

I have a below Jason format in which the Claims will be a array collection now I need to read all the Claims(which is a collection in below json) related to the policyid from c# , could anyone help me resoling it

{
                "ClaimList": [{
                                "Claims": ["CL8901"],
                                "PolicyID": "W1234sd",
                                "ClaimsCount": 1
                }, {
                                "Claims": ["CL3456", "CL1234"],
                                "PolicyID": "T1234sd",
                                "ClaimsCount": 2

                }, {
                                "Claims": ["CL1321"],
                                "PolicyID": "C1234sd",
                                "ClaimsCount": 1
                }]
}

4 Answers4

0
string json = Util.ReadLine<string>();
    JObject.Parse(json).SelectToken("ClaimList").Select(cl => new
    {
        PolicyId = cl.SelectToken("PolicyID"),
        Claims = cl.SelectToken("Claims").ToObject<List<string>>()
    }).Dump();

Linqpad code http://share.linqpad.net/vqoav5.linq

loneshark99
  • 706
  • 5
  • 16
0

To achieve what you need, do the following:

  1. Follow my answer here to create C# classes for your json
  2. Use NewtonSoft NuGet library to deserialize the JSON to C# objects
  3. Perform whatever processing you need on the deserialized items

Here is some code once you have done the above:

var claims = JsonConvert.DeserializeObject<Rootobject>("YourJsonHere");
IEnumerable<Claimlist> claimsForPolicy= 
    claims.ClaimList.Where(x => x.PolicyID == "whateverYouNeed");
Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
0

If you can use the Json.NET library (NuGet), this is pretty straightforward. First make sure you have a Claim class that has the structure of those inner objects in your JSON:

class Claim
{
    public List<string> Claims { get; set; }
    public string PolicyID { get; set; }
    public int ClaimsCount { get; set; }
}

Having that class available, you can use Json.NET to do something like this:

using Newtonsoft.Json;

// ...

string yourJson = getYourJsonStringSomehow();
var claimsDict =
    JsonConvert.DeserializeObject<Dictionary<string, List<Claim>>>(yourJson);

That will produce a Dictionary with one key: "ClaimsList", and the value will be a List of your Claim objects:

var claimsList = claimsDict["ClaimsList"];
var firstClaim = claimsList[0];
Console.WriteLine(firstClaim.PolicyID);
// Should print W1234sd
James McManus
  • 66
  • 1
  • 5
0

You can try this, first lets create a Json class for your json data:

public class JsonDoc
{
    //I am adding an index on constructor for looping purposes
    public JsonDoc(string json, int index)
    {
        JObject jObject = JObject.Parse(json);
        JToken jResult = jObject["ClaimList"];
        Claims = jResult[index]["Claims"].ToObject<string[]>();
    }

    //This constructor will return the number of count under ClaimList node
    public JsonDoc(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken jResult = jObject["ClaimList"][0];
        intCount = jResult.Count();
    }

    private string[] Claims { get; set; }
    public int intCount { get; set; }
}

Then to use this class for your Json data, first add a reference of Newtonsoft Json.

using Newtonsoft.Json.Linq;

Then on your sample data, I read all the data via file using StreamReader, loop on it and put all the data under a List:

private static void Main(string[] args)
    {
        using (StreamReader sr = new StreamReader(@"C:\Folder\myjson.json"))
        {
            string json = sr.ReadToEnd();
            JsonDoc jSon = new JsonDoc(json);

            //This will get the length of the array under ClaimList node
            int intCount = jSon.intCount;


            List<JsonDoc> lstResult = new List<JsonDoc>();
            for (int x = 0; x < intCount; x++)
            {
                lstResult.Add(new JsonDoc(json, x));
            }
        }
    }

The result is a List of JsonDoc class with array of string as part of property. This array of string is the result on your Claims Node. This JsonDoc class contains property Claims which is a string array which contains your Claims node:

enter image description here

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57