2

I have a Json serialized configuration in which I need to deserialize the object using

JsonConvert.DeserializeObject<>(jsonConfig)

to Myclass list. In jsonConfig there may be some properties missing where I got an exception like below.

Required property 'xxx' not found in JSON. Path '[0].yyy',

So is there any way to handle the undefined values while deserializing an object in c#?

1
  • Json.Net will only throw this exception if you have properties in your model which are marked as required, e.g. [JsonProperty(Required = Required.Always)]. So the obvious solution is to remove the requirement. Commented Sep 20, 2019 at 21:52

2 Answers 2

1

You need to make those properties nullable in object model class like below.

 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
 public int? someProperty { get; set; }

Also if property itself is missing from class while deserializing then you can do like below:

 JsonSerializerSettings settings = new JsonSerializerSettings();
 settings.MissingMemberHandling = MissingMemberHandling.Ignore;

 var deserializedObj = JsonConvert.DeserializeObject<MyModelClass>(jsonConfig, settings);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Rahul. this NullValueHandling is only for the property which has null value right. if the property itself missing means is there any way to handle or do we need any Custom JsonConverter for handling the undefined values?
it doesn't work for me still it's throwing the same exception
Please update your question and add your model class.
-1

undefined is not a valid json value, even though it is valid in javascript. Please check https://api.jquery.com/jQuery.parseJSON/

  • For json, use null instead of undefined: { "something": null }

1 Comment

A property is undefined in JSON if the property (including the property name) doesn't exist in the tree.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.