using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
MyData data = new MyData();
Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings
Converters = new List<JsonConverter>() { new TrimNullListValues() }
string jsonString = @"{""ListData"": [{""source"" : 10 , ""target"" : 20}, null]}";
JsonConvert.PopulateObject(jsonString, data, settings);
public class TrimNullListValues : JsonConverter
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
serializer.Serialize(writer, value);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
JArray array = JArray.Load(reader);
foreach (JToken item in array.ToList())
if (item.Type == JTokenType.Null)
return array.ToObject(objectType);
public override bool CanConvert(Type objectType)
return objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(List<>);
public class MyNestedData
public List<MyNestedData> ListData;