JsonException: The JSON value could not be converted to System.DateTime
means the date string in your JSON is in a shape the default DateTime
handling of System.Text.Json did not accept. The inner exception says so in plain words:
The JSON value is not in a supported DateTime format. Either send the date in
a format it reads, or register a small converter that parses the format you receive.
In the case reproduced here the API sent 06/11/2026 14:30 (day, month, year).
A JsonConverter<DateTime> that calls DateTime.ParseExact
with dd/MM/yyyy HH:mm, added to JsonSerializerOptions.Converters,
made the same JSON deserialize.
The error
Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $.BookedAt | LineNumber: 0 | BytePositionInLine: 52.
---> System.FormatException: The JSON value is not in a supported DateTime format.
at System.Text.Json.ThrowHelper.ThrowFormatException(DataType dataType)
at System.Text.Json.Utf8JsonReader.GetDateTime()
...
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at Program.<Main>$(String[] args) in C:\...\dt\Program.cs:line 17
Why it happens
The JSON was {"Name":"Maria Garcia","BookedAt":"06/11/2026 14:30"} and the
target property was public DateTime BookedAt { get; set; }. The stack trace
shows where it failed: Utf8JsonReader.GetDateTime() threw a
FormatException, and the serializer wrapped it in a
JsonException that names the property path ($.BookedAt) and the
byte position. The Path part of the message is the useful clue: it tells you
which property held the string the reader refused.
A string like 06/11/2026 is also ambiguous to a human reader: it can mean 6
November or 11 June. A converter that names the exact format removes the guess, and the
output below shows which reading it chose.
The fix
var options = new JsonSerializerOptions();
options.Converters.Add(new DayMonthYearConverter());
var fixedBooking = JsonSerializer.Deserialize<Booking>(json, options)!;
Console.WriteLine($"{fixedBooking.Name} {fixedBooking.BookedAt:yyyy-MM-dd HH:mm}");
Console.WriteLine(JsonSerializer.Serialize(fixedBooking, options));
...
public class DayMonthYearConverter : JsonConverter<DateTime>
{
private const string Format = "dd/MM/yyyy HH:mm";
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> DateTime.ParseExact(reader.GetString()!, Format, CultureInfo.InvariantCulture);
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString(Format, CultureInfo.InvariantCulture));
}
Running dotnet run -- fix printed:
Maria Garcia 2026-11-06 14:30
{"Name":"Maria Garcia","BookedAt":"06/11/2026 14:30"}
The first output line shows the date read as 6 November 2026, the second shows the
converter's Write method sending it back in the same shape, so a round trip
with the same API keeps its format. Pass the same options instance to every call
that reads or writes these bookings.
How it was reproduced
A console project from dotnet new console on .NET SDK 10.0.403
(net10.0, no extra packages; System.Text.Json is part of the framework), a
Booking class with a DateTime property and
JsonSerializer.Deserialize<Booking>(json) with default options. Run with
dotnet run for the error and dotnet run -- fix for the converter.
Frequently asked
- How do I deserialize a dd/MM/yyyy date with System.Text.Json?
- Write a JsonConverter<DateTime> whose Read method calls DateTime.ParseExact with the format dd/MM/yyyy HH:mm and CultureInfo.InvariantCulture, then add it to JsonSerializerOptions.Converters and pass those options to Deserialize.
- What does Path: $.BookedAt mean in a JsonException?
- It is the JSON path of the value that failed. $ is the root object and BookedAt is the property, so the string in BookedAt is the one the serializer could not turn into a DateTime.
- Why does the inner exception say not in a supported DateTime format?
- The serializer read the date with Utf8JsonReader.GetDateTime, which threw a FormatException for the string 06/11/2026 14:30. The outer JsonException wraps that FormatException and adds the path and position.
More decoded errors in the Fixes category. The same message for a number sent as text is covered in The JSON value could not be converted to System.Int32.