-1

I have a model with a nullable DateTime property

public DateTime? DateOfBirth { get; set; } 

when calling the API with the following value for the property

{ "Country": "US", "State": "New York", "Zip" : "123455", "dateOfBirth": "8/15/62" } 

This error is thrown by .net framework:

"errors": { "$.dateOfBirth": [ "The JSON value could not be converted to System.Nullable`1[System.DateTime]. Path: $.dateOfBirth | LineNumber: 9 | BytePositionInLine: 33." ] 

The parsing seem to work with other date formats like yyyy-mm-dd, but not for the format mentioned above in the example.

6
  • If you wonder whether it's the format, have you considered just calling it with a different format to verify your hypothesis? Commented Feb 2, 2022 at 21:42
  • @mason yes, I have, seems to work with other formats, but I wonder why this format is not supported while parsing Commented Feb 2, 2022 at 21:43
  • 1
    Okay, so you know the answer to the question you asked in your post. Perhaps you've asked the wrong question, and should edit your post to ask the thing that you actually want to know? Commented Feb 2, 2022 at 21:45
  • 1
    @mason updated the question, now do you have anything useful to mention? Commented Feb 2, 2022 at 21:48
  • You've updated the post...but now it lacks a question entirely. Stack Overflow is a question based site. What is it you want to know? Do you want to know how to post data in that format to your API? Or do you want to know why it doesn't work as written? Those are different questions, requiring different answers. Therefore, you need to be specific about what you're asking. I'm sorry if you feel my comments have not been helpful so far, but guiding you towards how to ask a good question is actually a very important thing. Commented Feb 2, 2022 at 21:50

1 Answer 1

1

I deserialized it using Newtonsoft.Json and had no errors in the processe. I used this class

public class Root { public string Country { get; set; } public string State { get; set; } public string Zip { get; set; } public DateTime? dateOfBirth { get; set; } } 

but my Windows CultureInfo("en-US"). if your culture info is different or are using another serializer and have a propblem I would just advice you to change DateTime type to string, and try to convert ToDateTime after deserialization.

DateTime format depends on regioanal culture in the Windows settings, so if datetime string format in US (as in your json) but you are in Europe, you will always get the error , so IMHO you better use this code, since you can select a culture of json, not Windows default.

public class Root { public string Country { get; set; } public string State { get; set; } public string Zip { get; set; } [JsonProperty("dateOfBirth")] private string DateOfBirth { get; set; } [JsonIgnore] public DateTime? dateOfBirth { get { return DateTime.Parse(DateOfBirth, new CultureInfo("en-US")); } } } 
Sign up to request clarification or add additional context in comments.

8 Comments

I am using .net's own deserializer from namespace System.Text.Json
@Sameed System.Text.Json is a garbage, you will have only problems. Change to Newtonsoft one.
@Serge I have never had a problem with System.Text.Json. This question is totally within System.Text.Json's use. Curious as to why you think that?
@Serge At least 100 lines? Sounds like you didn't know how to do it once and wrote it off forever. I've never found anything that couldn't be done with a custom converter (which abstracts the conversion away from your model resulting in cleaner code).
@aweyeahdawg Yes, this is what I mean. When I use Newtonsoft for complicated jsons I need a custom converter in 1 case of hundred. With Text.Json I need a custom converter in 99%
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.