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.

6
  • 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 Commented Oct 4, 2016 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 Commented Oct 4, 2016 at 11:44
  • @Learning Specific property or all null properties ? Commented Oct 4, 2016 at 11:46
  • @OrelEraki:Suppose if i want to ignore specific property then is it possible?? Commented Oct 4, 2016 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 and also this question… it’s still a duplicate. Commented Oct 4, 2016 at 12:08

1 Answer 1

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

3 Comments

You beat me to it. The OP should also look at: stackoverflow.com/questions/9819640/… This might help.
Ok this works but suppose if i am not using JsonConvert.SerializeObject object then how it is possible to do this??
I have upvoted your answer because it works with jsonConvert.SerializeObject

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.