How to handle null/empty values in JsonConvert.DeserializeObject
Solution 1
You can supply settings to JsonConvert.DeserializeObject
to tell it how to handle null values, in this case, and much more:
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
var jsonModel = JsonConvert.DeserializeObject<Customer>(jsonString, settings);
Solution 2
An alternative solution for Thomas Hagström, which is my prefered, is to use the property attribute on the member variables.
For example when we invoke an API, it may or may not return the error message, so we can set the NullValueHandling property for ErrorMessage:
public class Response
{
public string Status;
public string ErrorCode;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string ErrorMessage;
}
var response = JsonConvert.DeserializeObject<Response>(data);
The benefit of this is to isolate the data definition (what) and deserialization (use), the deserilazation needn’t to care about the data property, so that two persons can work together, and the deserialize statement will be clean and simple.
Solution 3
You can subscribe to the 'Error' event and ignore the serialization error(s) as required.
static void Main(string[] args)
{
var a = JsonConvert.DeserializeObject<DataTable>("-- JSON STRING --", new JsonSerializerSettings
{
Error = HandleDeserializationError
});
}
public static void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)
{
var currentError = errorArgs.ErrorContext.Error.Message;
errorArgs.ErrorContext.Handled = true;
}
Related videos on Youtube

Kyle
Updated on June 23, 2021Comments
-
Kyle over 2 years
I have the following code:
return (DataTable)JsonConvert.DeserializeObject(_data, (typeof(DataTable)));
Then, I tried:
var jsonSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; return (DataTable)JsonConvert.DeserializeObject<DataTable>(_data, jsonSettings);
The return line is throwing the error:
{"Error converting value \"\" to type 'System.Double'."}
Lots of solutions online suggesting creating custom
Class
with nullable types but this won't work for me. I can't expect the json to be in a certain format. I have no control over the column count, column type, or column names.-
Jon SkeetPerhaps you should just use LINQ to JSON instead, and get a
JObject
instead of creating aDataTable
? -
dbcCan you share an example of the JSON that is causing the problem?
-
-
Kyle over 8 yearsThis ignores the error all right. But the
DataTable
object returned is null. -
DotNetHitMan over 8 yearsCan you post the JSON string and then i will alter my answer to better suit.
-
iTrout about 7 yearsThis is the perfect answer - I've been scouring solutions and finally one that works. Hero status.
-
Saskia about 5 yearsMissingMemberHandling = MissingMemberHandling.Ignore is default. No need to add it.
-
Satheesh K over 3 yearsI got an error when deserializing the primitive type (long, int) properties in my class. The json comes from different api which i cannot control. The generic solution worked for me. Thank you Thomas
-
vivek86 almost 3 yearswithout context about what services you are accessing, the answer would be both incomplete and wrong.
-
SendETHToThisAddress over 2 yearsimo these should be the default settings for the Deserializer. It shouldn't bug out and break your code by default just because a null value was encountered
-
M Fuat over 2 years@vivek86 I have updated the answer. You can remove your downvote.
-
ZXX almost 2 yearsThis is actually very good answer for contyemporary code since the probelm of setting global JSON deserialization options is surfacing up a lot.