0

I have 1 class like below:

public class Tables
    {
        public string Name { get; set; }
        public string[] Columns { get; set; }
    }

string[] selectedTables = { "Table1", "Table2"};
using (var conection = new SqlConnection("MyconnectionString"))
{
    connection.Open();

    var tables = (
        from table in connection.GetSchema("Tables").AsEnumerable()
        let name = (string)table["TABLE_NAME"]
        where selectedTables.Contains(name)
        let catalog = (string)table["TABLE_CATALOG"]
        let schema = (string)table["TABLE_SCHEMA"]
        select new Tables // this should really be called Table
        {
            Name = name,
            Columns = (
                from column in connection.GetSchema("Columns", new [] { catalog, schema, name }).AsEnumerable()
                select (string)column["COLUMN_NAME"]).ToArray()
        }).ToList();

    return tables;
}

Here i am creating response to return data to service:

     var response = tables.
                      Select
                   (
                        t => new
                          {
                            id = t.Id,
                            tables = t.Tables
                            .Select
                            (
                              x => new
                               {
                                   name = x.Name,
                                   columns = x.Columns 
                               }
                             ).ToList()
                          }
                    ).ToList();

  return Json(response);

Below is my JSON:

Output

Now when generating columns data for each tables if columns is null for suppose Table1 then I don't want this columns field to be present in my JSON.

Is this possible because I follow this Question in which it is written that it is not possible.

Update: I am using asp.net mvc so I am doing like this to create json - is that possible?

return Json(response);

This is not duplicate question as I am using JSON class of MVC to generate JSON result.

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 1
    Where is the code that generates the Json string? Without it, the rest isn't relevant. In fact, you could remove all the database related code and use a test object and the serialization code – Panagiotis Kanavos Oct 04 '16 at 11:33
  • @PanagiotisKanavos I have updated my question to include the code which generate json response.i am usin JsonConvert.SerializeObject to create json response – I Love Stackoverflow Oct 04 '16 at 11:44
  • @Learning Specific property or all null properties ? – Orel Eraki Oct 04 '16 at 11:46
  • @OrelEraki:Suppose if i want to ignore specific property then is it possible?? – I Love Stackoverflow Oct 04 '16 at 11:52
  • 4
    “What are you using to generate the JSON?” – “I’m using JSON.NET!” – *solution for JSON.NET* – “No wait, I actually lied. You are so wrong about it, this is not a duplicate!!!” – Seriously? Anyway, see [this question](http://stackoverflow.com/q/20974811/216074) and also [this question](http://stackoverflow.com/q/13294211/216074)… it’s still a duplicate. – poke Oct 04 '16 at 12:08
  • @poke Sorry for the inconvinience actually i havent specified asp.net mvc tag and JsonConvert.SerializeObject is very common that is why i posted JsonConvert.SerializeObject.If i would have posted Json then some users would have confused that what is json as it is a class in asp.net mvc for creating json response – I Love Stackoverflow Oct 04 '16 at 12:12

1 Answers1

5

You can tell the Json serializer to ignore empty properties like this:

JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { 
                                NullValueHandling = NullValueHandling.Ignore
                            });
Rob
  • 11,492
  • 14
  • 59
  • 94