1

I want to know how to filter JSON data. Below is my data:

[
    {
        "admno": "VCN-2015-0005",
        "subcode": "L001",
        "subjectname": "Mathematics",
        "paper": 1,
        "markscored": 48,
        "grade": "E",
        "tr_comments": "Emmanuel can do better with improved participation in class, more practice and proper usage of his time especially during prep time.",
        "studyclass": 7,
        "term": 3,
        "study_year": "2015/2016"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L001",
        "subjectname": "Mathematics",
        "paper": 1,
        "markscored": 45,
        "grade": "E",
        "tr_comments": "Emmanuel's behavior has improved and has registered a good class attendance. This score is low but he is capable of making it better. This subject requires practice. He is therefore advised to engage in constant revision and practice and doing his assignments independently in addition to meeting deadlines for assignments.",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L001",
        "subjectname": "Mathematics",
        "paper": 1,
        "markscored": 50,
        "grade": "D",
        "tr_comments": "Emmanuel, you have the potential to do better than this but your effort is still minimal. The subject has several concepts which one should understand , master and should be able to apply the knowledge in algebra, number, geometry handling data and problem solving. It therefore calls for daily continuous practice if you are to perform well.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L002",
        "subjectname": "English",
        "paper": 1,
        "markscored": 45,
        "grade": "E",
        "tr_comments": "Jessy,this is below average,you need to settle down for hard work and improve on this grade.",
        "studyclass": 7,
        "term": 3,
        "study_year": "2015/2016"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L002",
        "subjectname": "English",
        "paper": 1,
        "markscored": 28,
        "grade": "G",
        "tr_comments": "Kisakye has a number of challenges including grammar, reading comprehension, spellings and written expression. He is encouraged to give extra time for more lessons and to take better care of his property.",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L002",
        "subjectname": "English",
        "paper": 1,
        "markscored": 36,
        "grade": "F",
        "tr_comments": "Emmanuel is capable of performing better than this but he still has challenges right from paying attention in class to completing given assignments, which denies him the much needed practice to improve his reading comprehension and writing.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L003",
        "subjectname": "Combined Science",
        "paper": 1,
        "markscored": 68,
        "grade": "C",
        "tr_comments": "Kimbugwe is interested in the subject and this is reflected in the way he asks questions during our lessons. He has a lot of scientific information in his head but he finds a challenge putting it together. With guidance he will get there. Keep up the participation Jesse. ",
        "studyclass": 7,
        "term": 3,
        "study_year": "2015/2016"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L005",
        "subjectname": "Environmental Science",
        "paper": 1,
        "markscored": 53,
        "grade": "D",
        "tr_comments": "Emmanuel shows a lot of interest in his studies.He actively contributes during class discussion...this explains his performance.keep it up!",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L005",
        "subjectname": "Environmental Science",
        "paper": 1,
        "markscored": 30,
        "grade": "F",
        "tr_comments": "Kimbugwe has to improve on his revision skill and adequately prepare for the exam so as to yield better marks",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L006",
        "subjectname": "Information Technology",
        "paper": 1,
        "markscored": 65,
        "grade": "C",
        "tr_comments": "Emma's grade is fair, but he rushed in the end of term exam. The grade was really below his potential so he sat again and got this better grade. He should always prepare himself well and do his papers without rushing.",
        "studyclass": 7,
        "term": 3,
        "study_year": "2015/2016"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L006",
        "subjectname": "Information Technology",
        "paper": 1,
        "markscored": 48,
        "grade": "E",
        "tr_comments": "Emmanuel needs to focus more. He had to re-do this test to improve. He also needs to always keep his books safer.",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L006",
        "subjectname": "Information Technology",
        "paper": 1,
        "markscored": 76,
        "grade": "B",
        "tr_comments": "Emma has not had a book for a considerable part of the term. This slightly affected revision. He also needs to be more attentive during lessons. The on and off episodes of his should really stop.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L008",
        "subjectname": "Art & Design",
        "paper": 1,
        "markscored": 90,
        "grade": "A*",
        "tr_comments": "This is an excellent performance. We encourage you to continue with the effort and take more time learning the different terminologies used in design technology. Also continue trying out new ideas, experimenting with paints and other colours.",
        "studyclass": 7,
        "term": 3,
        "study_year": "2015/2016"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L008",
        "subjectname": "Art & Design",
        "paper": 1,
        "markscored": 70,
        "grade": "B",
        "tr_comments": "Emmanuel has shown some progress in his performance. He is an inquisitive learner who enjoys analyzing situations and making new discoveries in art. He is a self-motivated and hard working. I encourage him to maintain the progress and even do consultations so as to aspire for excellent results\r\n",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L008",
        "subjectname": "Art & Design",
        "paper": 1,
        "markscored": 70,
        "grade": "B",
        "tr_comments": "-",
        "studyclass": 8,
        "term": 2,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L008",
        "subjectname": "Art & Design",
        "paper": 1,
        "markscored": 55,
        "grade": "D",
        "tr_comments": "Emmanuel is a learner with great energies who requires plenty of attention in form of follow ups. He is encouraged to be self motivated and stay focused an the task at hand.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L009",
        "subjectname": "Business & Enterprise",
        "paper": 1,
        "markscored": 40,
        "grade": "E",
        "tr_comments": "Emmanuel's performance is below average. He is capable of performing better than this only that he is not settled, doesn't listen and follow directions. He is slow at accomplishing assignments and is also easily destructed by friends. He is advised to improve on his work habits if he is to perform better.",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L009",
        "subjectname": "Business & Enterprise",
        "paper": 1,
        "markscored": 51,
        "grade": "D",
        "tr_comments": "-",
        "studyclass": 8,
        "term": 2,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L009",
        "subjectname": "Business & Enterprise",
        "paper": 1,
        "markscored": 55,
        "grade": "D",
        "tr_comments": "Emmanuel, you are trying to make an improvement on your performance. This is promising However, more efforts need to be committed to your studies first if you are to perform better than this.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L010",
        "subjectname": "French",
        "paper": 1,
        "markscored": 52,
        "grade": "D",
        "tr_comments": "Emmanuel is not familiar with enough grammar at this level, though he has absorbed new material effectively. His knowledge and understanding of different structures needs to be increased. However, he is able to make use of what he knows in productive tasks.",
        "studyclass": 7,
        "term": 3,
        "study_year": "2015/2016"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L010",
        "subjectname": "French",
        "paper": 1,
        "markscored": 28,
        "grade": "G",
        "tr_comments": "Emmanuel needs to consult his teachings to catch up on work misses.He seems to have missed out on a lot of work.Otherwise,he has all the potential to perform better in french",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L010",
        "subjectname": "French",
        "paper": 1,
        "markscored": 0,
        "grade": "U",
        "tr_comments": "-",
        "studyclass": 8,
        "term": 2,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L010",
        "subjectname": "French",
        "paper": 1,
        "markscored": 41,
        "grade": "E",
        "tr_comments": "Emma could do better really! He has the potential but he lacks consistent concentration and focus. He comes out as a serious student in one lesson, and and a very playful and distractive student in the other. Inconsistency and playfulness. He likes making fun in class.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L011",
        "subjectname": "Health & Physical Education",
        "paper": 1,
        "markscored": 87,
        "grade": "A",
        "tr_comments": "Jessy has greatly improved on his performance this term: He always completes his assignments and classwork in time. However he some times comes late in class because of his love for computer games. We have talked about this and he promises to change for the better.",
        "studyclass": 7,
        "term": 3,
        "study_year": "2015/2016"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L011",
        "subjectname": "Health & Physical Education",
        "paper": 1,
        "markscored": 77,
        "grade": "B",
        "tr_comments": "Jessy Emmanuel enjoys and always attends the H&PE lessons most especially the practicals. He has improved on his pace when writing his notes. He rarely puts on his proper P.E attire; he is encouraged to work on his weak areas like revising his notes for a better grade in next term. ",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L011",
        "subjectname": "Health & Physical Education",
        "paper": 1,
        "markscored": 56,
        "grade": "D",
        "tr_comments": "-",
        "studyclass": 8,
        "term": 2,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L011",
        "subjectname": "Health & Physical Education",
        "paper": 1,
        "markscored": 66,
        "grade": "C",
        "tr_comments": "Jessy has slightly declined in his performance this term: He always completes his assignments and classwork in time. However in practicals, he rarely has an appropriate sports attire and this affects his practicals. He is encouraged to get an appropriate sports attire.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L012",
        "subjectname": "Combined Science (Biology)",
        "paper": 1,
        "markscored": 58,
        "grade": "D",
        "tr_comments": "A fair attempt Emma, but you could have done better if you settled, and did more personal reading and sturdy.",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L012",
        "subjectname": "Combined Science (Biology)",
        "paper": 1,
        "markscored": 88,
        "grade": "A",
        "tr_comments": "-Emmanuel has improved in the subject.But we encourage him to remain focused and avoid those habits that can make him decline in future.\n",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L013",
        "subjectname": "Combined Science (Chemistry)",
        "paper": 1,
        "markscored": 63,
        "grade": "C",
        "tr_comments": "This score is fair and he has potential to improve it. He should be encouraged to start reading hard before the work accumulates. He is advised do extensive personal reading to improve on this grade. I have no doubt he will make it if he does this plus utilizing the teachers.",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L013",
        "subjectname": "Combined Science (Chemistry)",
        "paper": 1,
        "markscored": 70,
        "grade": "B",
        "tr_comments": "-",
        "studyclass": 8,
        "term": 2,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L013",
        "subjectname": "Combined Science (Chemistry)",
        "paper": 1,
        "markscored": 73,
        "grade": "B",
        "tr_comments": "-Emmanuel has improved on his attitude towards studies.We encourage him to keep on track for better performance in future.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L014",
        "subjectname": "Combined Science (Physics)",
        "paper": 1,
        "markscored": 70,
        "grade": "B",
        "tr_comments": "Encouraging performance Emmanuel. Put in more effort for a much better performance. Keep consulting the teacher for effective understanding of the subject.",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L014",
        "subjectname": "Combined Science (Physics)",
        "paper": 1,
        "markscored": 70,
        "grade": "B",
        "tr_comments": "-",
        "studyclass": 8,
        "term": 2,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L014",
        "subjectname": "Combined Science (Physics)",
        "paper": 1,
        "markscored": 60,
        "grade": "C",
        "tr_comments": "A noticeable improvement has been registered by Kimbugwe as a result of his being more active in class as well as constant teacher supervision.He is advised to do more practice so as to perform much better as he goes to y9 and also learn to work independently.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L015",
        "subjectname": "Personal, Social & Health Education",
        "paper": 1,
        "markscored": 55,
        "grade": "D",
        "tr_comments": "Emma shows interest in the subject and endeavors to take his notes however he is advised to do more listening than talking during the lesson to attain better a grade.",
        "studyclass": 8,
        "term": 1,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L015",
        "subjectname": "Personal, Social & Health Education",
        "paper": 1,
        "markscored": 48,
        "grade": "E",
        "tr_comments": "-",
        "studyclass": 8,
        "term": 2,
        "study_year": "2016/2017"
    },
    {
        "admno": "VCN-2015-0005",
        "subcode": "L015",
        "subjectname": "Personal, Social & Health Education",
        "paper": 1,
        "markscored": 68,
        "grade": "C",
        "tr_comments": "Emma shows interest in the subject and endeavors to take his notes however he is advised to do more personal reading and utilize his preps to attain a better grade.",
        "studyclass": 8,
        "term": 3,
        "study_year": "2016/2017"
    }
]

I want to filter this List by study_year and term. For instance, I may want to extract results when, e.g., study_year == 2016/2017 and term == 2, to bring the JSON data with only that information.

How can i do this in C#? This is my C# code but doesn't work.

 var resultObject = localList[].Values < OnResultsList<JObject>().Select(new["study_year].Value<String>() =="2016/2017");

localList is where My JSON Object stored.

9
  • 1
    Have you tried anything? Commented Sep 5, 2017 at 11:38
  • stackoverflow.com/questions/12676746/… c-sharpcorner.com/article/working-with-json-string-in-C-Sharp Commented Sep 5, 2017 at 11:42
  • yeaah i did , but still doesnt work , let me update my post Commented Sep 5, 2017 at 11:43
  • 1
    people who marked my question duplicate , i guess they didn't read through it , i meant C# not javascript. Commented Sep 5, 2017 at 11:52
  • 1
    Normally, one would use .Where to filter a list. Plus, you can't just < on a list either, so not sure what you are doing there. Commented Sep 5, 2017 at 17:38

1 Answer 1

1

You could install Json.NET if you have not done so already (your c# code uses the type JObject so perhaps you already have), then load your JSON into a LINQ to JSON JToken hierarchy, then query the hierarchy using Enumerable.Where() to filter and select array items:

var root = JToken.Parse(jsonString);

var study_year = "2016/2017";
var term = 2;
var values = root.Where(t => (int?)t["term"] == term && (string)t["study_year"] == study_year).ToList();

Alternatively, you could use SelectTokens() with the JSONPath query syntax

var queryString = string.Format("$[?(@.term == {0} && @.study_year == '{1}')]", term, study_year);
var values = root.SelectTokens(queryString).ToList();

A LINQ to JSON hierarchy is an in-memory, tokenized and parsed representation of a JSON string. After having filtered your JToken hierarchy, to re-serialize to a JSON string, you can do:

var filteredJsonString = JsonConvert.SerializeObject(values, Formatting.Indented);

Notes:

  • In the first query, I cast t["term"] to an int? rather than an int so that, in case the token "term" is missing, a null is returned, rather than an exception thrown.

  • In the second query, the JSONPath query string is:

    $[?(@.term == 2 && @.study_year == '2016/2017')]
    

    The value of study_year must appear in single quotes because the actual values in the JSON are strings, while the value of term must not because the actual values in the JSON are integers.

Sample fiddle showing both options.

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

2 Comments

Thanks @dbc for the answer , but does var values = root.Where(t => (int?)t["term"] == term && (string)t["study_year"] == study_year).ToList(); , has a sorted Format string JSON ? or , because i want it to come when its still in JSON format after being sorted.
@IdrisStack - If you're asking how to re-convert the filtered JToken list to a JSON string, do var filteredJsonString = JsonConvert.SerializeObject(values, Formatting.Indented);. I edited my question to include this. If not, I'm having some trouble understanding your English, so can you edit your question to show what you want to accomplish? Your current code doesn't even compile. A minimal reproducible example would be ideal. See also How to Ask.

Your Answer

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