Click or drag to resize
Json.NETNullValueHandling setting
 

This sample serializes an object to JSON with NullValueHandling set to Ignore so that properties with a default value aren't included in the JSON result.

Sample
Types
1public class Person
2{
3    public string Name { get; set; }
4    public int Age { get; set; }
5    public Person Partner { get; set; }
6    public decimal? Salary { get; set; }
7}
Usage
 1Person person = new Person
 2{
 3    Name = "Nigal Newborn",
 4    Age = 1
 5};
 6
 7string jsonIncludeNullValues = JsonConvert.SerializeObject(person, Formatting.Indented);
 8
 9Console.WriteLine(jsonIncludeNullValues);
10// {
11//   "Name": "Nigal Newborn",
12//   "Age": 1,
13//   "Partner": null,
14//   "Salary": null
15// }
16
17string jsonIgnoreNullValues = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
18{
19    NullValueHandling = NullValueHandling.Ignore
20});
21
22Console.WriteLine(jsonIgnoreNullValues);
23// {
24//   "Name": "Nigal Newborn",
25//   "Age": 1
26// }