Extracting a list of objects from a repository as JSON Currently, I am using the below code to make JSON data from list of objects.

public IActionResult getWardsFromVDC(long vdc_id)
{
    try
    {
        List&lt;Ward&gt; wards = wardRepo.getByVdcId(vdc_id).ToList();
        List&lt;Dictionary&lt;string, string&gt;&gt; values = new List&lt;Dictionary&lt;string, string&gt;&gt;();

        foreach (var ward in wards)
        {
            Dictionary&lt;string, string&gt; value = new Dictionary&lt;string, string&gt;();
            value["ward_id"] = ward.ward_id.ToString();
            value["ward_name"] = ward.ward_name;
            values.Add(value);
        }
        Dictionary&lt;string, object&gt; data = new Dictionary&lt;string, object&gt;();
        data["data"] = values;

        var json = JsonConvert.SerializeObject(data);
        return Json(json);
    }
    catch (Exception ex)
    {
        return Json(ex.Message);
    }
}



List&lt;Ward&gt; wards = wardRepo.getByVdcId(vdc_id).ToList();



is getting list of Ward class from database. Then a loop is used to put all the values in List of Dictionaries . 


 Dictionary&lt;string, object&gt; data = new Dictionary&lt;string, object&gt;();
 data["data"] = values;

 var json = JsonConvert.SerializeObject(data);



is putting all those values in "data" key in another dictionary and then value is serialized and returned as JSON

The JSON data formed is :

{
  "data": [
    {
      "ward_id": "132",
      "ward_name": "1"
    },
    {
      "ward_id": "133",
      "ward_name": "2"
    },
    {
      "ward_id": "134",
      "ward_name": "3"
    },
    {
      "ward_id": "135",
      "ward_name": "4"
    },
    {
      "ward_id": "136",
      "ward_name": "5"
    },
    {
      "ward_id": "137",
      "ward_name": "6"
    },
    {
      "ward_id": "138",
      "ward_name": "7"
    },
    {
      "ward_id": "139",
      "ward_name": "8"
    },
    {
      "ward_id": "140",
      "ward_name": "9"
    },
    {
      "ward_id": "141",
      "ward_name": "10"
    },
    {
      "ward_id": "142",
      "ward_name": "11"
    },
    {
      "ward_id": "143",
      "ward_name": "12"
    }
  ]
}


To build even a simple data , I am creating dictionary and list of dictionaries and again adding data key . 

Is there any simple way to build JSON in same format but in simplest way?
Anonymous types are going to be your friend here.  You don't need to have list or dictionary at all.   

First you don't need to call ToList() from your repository.

Now we can just project out the properties you want like so

wards.Select(w =&gt; new
{
     ward_id = w.ward_id.ToString(),
     w.ward_name
})


JsonConvert will serialize an IEnumerable as an array.  this will get your square bracket around the "Wards" and have the two properties you would like and having the Id converted to a string. 

Now you just need the "data" object to contain the IEnumerable.  Again making an anonymous type.

var data = new
{
    data = wards.Select(w =&gt; new
    {
        ward_id = w.ward_id.ToString(),
        w.ward_name
    })
};

var json = JsonConvert.SerializeObject(data);


That's it.  That should get you what you want.