4

I am new on json in C#. I use newtonsoft.json I have a json file with data (array):

[ 
    {
         "firstName": "Joyce",
         "lastName": "Huff",
         "isActive": true,
         "age": 59,
         "gender": "female",
         "eyeColor": "green",
         "friends": [
                        "Kendra Buck"
                    ]
   },
   {
        "firstName": "Diann",
        "lastName": "Patrick",
        "isActive": true,
        "age": 45,
        "gender": "female",
        "eyeColor": "blue",
        "friends": [
                      "Roach Mills",
                      "Diaz Pickett"
                   ]
   },
   {
       "firstName": "Holt",
       "lastName": "Erickson",
       "isActive": false,
       "age": 53,
       "gender": "male",
       "eyeColor": "brown",
       "friends": [
                    "Lindsay Wyatt",
                    "Freeman Mcfadden",
                    "Matilda Franklin"
                  ]
  },
  {
      "firstName": "Crystal",
      "lastName": "Santiago",
      "isActive": false,
      "age": 31,
      "gender": "female",
      "eyeColor": "brown",
      "friends": [
                   "Stacy Joseph"
                 ]
   }
]

How to I read a json file containing array with C# and perform LINQ query on it? I found example on JObject to read json from file but I could not figure it out how do I handle json array. After reading json array, I would like to run query like: select count(*) from person where age>40;

Please suggest me. Thank you in advance.

aliceangel
  • 304
  • 1
  • 5
  • 19

2 Answers2

7

Define model:

public class Person
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public bool isActive { get; set; }
    public int age { get; set; }
    public string gender { get; set; }
    public string eyeColor { get; set; }
    public List<string> friends { get; set; }
}

Read and deserialize JSON:

string json = System.IO.File.ReadAllText("test.json");
var people = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Person>>(json);

Perform LINQ query:

var peopleOverForty = from p in people
                      where p.age > 40
                      select p;
Alex
  • 1,433
  • 9
  • 18
  • Thank you very much for giving me an example. Can you please explain me if the json array data is coming from web api continuously and the size is really large like gigabyte, how could I handle it? – aliceangel Mar 14 '18 at 09:41
2

I would suggest creating a Class for the Object you're trying to read, if possible at least.

Then I would deserialize the JSON String to an List<T>where T euqals your Modelclass.

List<YourObject> deserializedObject = JsonConvert.DeserializeObject<YourObject>(jsonString);

Wit this list you can then easily perform LINQ queries like

List<YourObject> selectedObjects = deserializedObject.Where(x => x.age > 31);

This gives you the object selectedObjects with only containing Objects where age > 31.

Vulpex
  • 1,041
  • 8
  • 19