Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count the total sub nodes in a JSON using Newtonsoft.Json

I am trying to find the count of the total nodes inside a file which is in JSON format. I tried the below code and it is not working. Googled few but couldn’t find what I am looking for. I am new to JSON file handling, please guide me.

Json Input 1:

    {
        "CandidateId": "E3",
        "Ngocentre": "Chennai",
        "FirstName": "XXX",
        "LastName": "YYY",
        "PhoneNumber": 22221,
        "EmailId": "E3@gmail.com",
        "EducationalQualification": "Graduate",
        "SkillSet": "ASP.NET",
        "CreatedByNgo": "Gurukul",
        "UpdatedByNgo": "Gurukul",
        "CreatedDate": "0001-01-01T00:00:00",
        "ModifiedDate": "0001-01-01T00:00:00",
        "NgoemailId": "gurukul@gmail.com"
    }

** Json Input 2:**

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    [
    {
        "CandidateId": "E3",
        "Ngocentre": "Chennai",
        "FirstName": "XXX",
        "LastName": "YYY",
        "PhoneNumber": 22221,
        "EmailId": "E3@gmail.com",
        "EducationalQualification": "Graduate",
        "SkillSet": "ASP.NET",
        "CreatedByNgo": "Gurukul",
        "UpdatedByNgo": "Gurukul",
        "CreatedDate": "0001-01-01T00:00:00",
        "ModifiedDate": "0001-01-01T00:00:00",
        "NgoemailId": "gurukul@gmail.com"
    },
        {
        "CandidateId": "E3",
        "Ngocentre": "Chennai",
        "FirstName": "XXX",
        "LastName": "YYY",
        "PhoneNumber": 22221,
        "EmailId": "E3@gmail.com",
        "EducationalQualification": "Graduate",
        "SkillSet": "ASP.NET",
        "CreatedByNgo": "Gurukul",
        "UpdatedByNgo": "Gurukul",
        "CreatedDate": "0001-01-01T00:00:00",
        "ModifiedDate": "0001-01-01T00:00:00",
        "NgoemailId": "gurukul@gmail.com"
    }
    ]

My ideal output should be "1" for the 1st input and "2" for the 2nd input. I tried the below code.

using (StreamReader r = new StreamReader(@"C:\Users\xxx\Desktop\Read.txt"))
            {
                string json = r.ReadToEnd();
                dynamic source = JsonConvert.DeserializeObject(json);
                int count = ((Newtonsoft.Json.Linq.JContainer)((Newtonsoft.Json.Linq.JToken)source).Root).Count;
            }

But when pass only the 1st input, I am getting count as 13 which is the total properties inside a Json.
Above code piece returns 2 for the 2nd input which is the ideal output, but it fails when I pass 1st input alone.

>Solution :

you can just count the tokens :

    if (json.StartsWith("["))
    {
        // If the json is an array
        var array = JArray.Parse(json);
        Console.WriteLine(array.Count);
    }
    else
    {
        // If the json is only one object, the result is 1.
        // I added a way to retieve the object as token array :
        var jobject = JObject.Parse(json);
        var tokens = jobject.SelectTokens("$"); // You can go through you object : jobject.SelectTokens("$.CandidateId") => "E3"
        Console.WriteLine(tokens.Count());
    }

An other solution would be the Regex with atomic group.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading