System.Text.Json is case-sensitive: how it silently dropped stored data
By default, System.Text.Json matches property names case-sensitively, so JSON stored as {"Theme": "..."} does not bind to a property mapped to "theme". It throws nothing: the property keeps its default value and the rest of the object deserialises fine. If the JSON was written by a different component than the one reading it, pass JsonSerializerOptions with PropertyNameCaseInsensitive = true (or JsonSerializerDefaults.Web) on the read, and cache that options instance.
What went missing
I work on a .NET pipeline that generates LLM summaries in stages. One stage asks a model for a structured result, a short list of themes, each with a theme name and a detail sentence, and stores it as JSON in a database row. A later stage reads that row back, merges in output from a second model call, and writes the merged document over the original.
After the later stage ran, the lists of themes were gone. The first stage had clearly produced them, because they were in the row before the second stage touched it. Nothing in the logs said anything had failed. The merge ran, returned a valid document and saved it.
Why no exception was thrown
The two stages used different types for the same data. The writer serialised the model's response type with default options, which keep .NET property names as they are, so the row held PascalCase keys:
{"Items":[{"Theme":"Clear ownership","Detail":"Teams decide quickly."}]}
The reader deserialised into its own DTO, whose properties were mapped to camelCase names with [JsonPropertyName], because that is the shape the rest of the system writes and serves:
public sealed class ThemeDto
{
[JsonPropertyName("theme")]
public string? Theme { get; set; }
[JsonPropertyName("detail")]
public string? Detail { get; set; }
}
public sealed class SummaryDto
{
public List<ThemeDto> Items { get; set; } = [];
}
With default options, "Items" matched (the C# name, no attribute), so the list came back with the right number of entries. Inside each entry, "Theme" did not match "theme", so both fields stayed null. System.Text.Json ignores JSON properties it can't map unless you opt into JsonUnmappedMemberHandling.Disallow, so there was no error to catch. The merge then serialised those empty entries and saved them over the good data.
This is a read-modify-write, and that is what made it destructive. A read-only consumer with this bug shows a blank field on a page. One that writes back erases the source of truth.
Why it worked everywhere else
This catches people because the same JSON works through ASP.NET Core. MVC controllers and minimal APIs use JsonSerializerDefaults.Web, which turns on camelCase naming and case-insensitive matching. Newtonsoft.Json, which many codebases used before, is case-insensitive by default too. A bare JsonSerializer.Deserialize<T>(json) gets neither. So the DTO looked right, tests that went through the API looked right, and only the direct read from storage was strict.
A minimal reproduction
This console program shows both behaviours side by side:
using System.Text.Json;
using System.Text.Json.Serialization;
const string stored = """{"Items":[{"Theme":"Clear ownership","Detail":"Teams decide quickly."}]}""";
var strict = JsonSerializer.Deserialize<SummaryDto>(stored)!;
Console.WriteLine(strict.Items[0].Theme ?? "(null)"); // (null)
var lenient = JsonSerializer.Deserialize<SummaryDto>(stored, ReadOptions.CaseInsensitive)!;
Console.WriteLine(lenient.Items[0].Theme); // Clear ownership
static class ReadOptions
{
public static readonly JsonSerializerOptions CaseInsensitive =
new() { PropertyNameCaseInsensitive = true };
}
public sealed class ThemeDto
{
[JsonPropertyName("theme")] public string? Theme { get; set; }
[JsonPropertyName("detail")] public string? Detail { get; set; }
}
public sealed class SummaryDto
{
public List<ThemeDto> Items { get; set; } = [];
}
Case-insensitive matching applies to names set with [JsonPropertyName] as well as to C# property names, so the attribute and the option work together.
Fix the reader or fix the writer?
The tidy-sounding fix is to make the writer use the same naming policy as everyone else. That would be worth doing, but it doesn't help on its own: rows already written with PascalCase keys stay that way, and the reader still has to handle them. Changing the writer also changes a contract other readers might depend on.
So the fix went on the read side: both places that deserialised the stored document now pass case-insensitive options. The writer's output is unchanged and the merged document is still written in camelCase, so data written after the fix is consistent going forward while old rows still read correctly.
In code review, the first version of the change declared its own static options field in each class. The second reused a shared helper that already built case-insensitive options, so the read rule lives in one place. Wherever the options live, don't build them per call. JsonSerializerOptions holds the serializer's metadata cache, so each new instance pays the reflection cost again. Create it once, as a static readonly field or a shared helper, and reuse it. On .NET 9 and later, JsonSerializerOptions.Web is a ready-made read-only instance with the web defaults.
How to test it so it stays fixed
The useful test feeds the reader the exact shape the other component writes, PascalCase keys included, and asserts that the values come out the other side after the merge. A test that builds its input by serialising the reader's own DTO would never fail, because it round-trips through the same naming the reader expects. Pin the producer's shape as a literal string in the test, with a one-line comment saying which writer it mirrors.
If you want the serializer to fail loudly instead, JsonUnmappedMemberHandling.Disallow (.NET 8+) throws when the JSON has a property the type doesn't map. That would have turned this into an exception on the first run. It is too strict for documents that legitimately carry extra fields, but it is a good fit for tests.
Key takeaways
JsonSerializer.Deserializewithout options matches property names case-sensitively and ignores unmatched properties, so a casing mismatch produces empty fields, not an error.- ASP.NET Core and Newtonsoft.Json are case-insensitive by default, which hides the problem until something calls
System.Text.Jsondirectly. - When one component reads JSON another component wrote, set
PropertyNameCaseInsensitive = trueon the read rather than relying on both sides agreeing on casing. - Cache
JsonSerializerOptionsin a static field or shared helper, because each new instance rebuilds the serializer's metadata cache. - Test a read-modify-write step with the producer's literal stored JSON, not with JSON serialised from the consumer's own types.