I need to create different JSON in C#. Sometimes I need to create a Json with specific attributes of a class, and sometimes I need to create a JSON with all the attributes of the class But I don’t know how to delete some attributes(if the attribute is null or empty, I don’t need to create it in the Json, but it the attribute has a value I need it) Example
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string json = JsonConvert.SerializeObject(product);
With that example I create a JSOn with all the attributes , but how can I create A JSON without Price for example? Or if the attribute is a List I need to check if some object is null, don’t create the JSON with the objet in null
Thank you so much