5

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.

9
  • 1
    newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm Commented Mar 14, 2018 at 9:22
  • @BehrouzMoslem the link you provided, I visited already, It shows the JObject not JArray Commented Mar 14, 2018 at 9:26
  • Here is the same question and answer Commented Mar 14, 2018 at 9:27
  • @Sabrina you can easily use JArray instead of JObject and that's shown in most links. Even JObject works, it returns an object that contains an arrya. What have you tried anyway? Commented Mar 14, 2018 at 9:28
  • @PanagiotisKanavos I tried with JArray but I got array key/value problem, my program runs but no output is shown. It would be really very helpful, if you provide any code example. Commented Mar 14, 2018 at 9:33

2 Answers 2

9

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;
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.