JSON Serialization and Web API

May 2021

.NET Core Web API projects can be great for quick APIs that involve .NET-structured data. But sometimes mixing .NET objects and JSON doesn't behave as one might expect.

.NET Core 5.0 uses a Microsoft-provided library, System.Text.Json, to provide Json content in responses, ref here.

However, the keys in the actual Json response are camel-cased - they don't follow the same casing as the names of the .NET properties that were serialized.

If a deserializer on the receiving end is expecting Json keys that match a .NET object exactly - including casing - it won't be able to deserialize the Json response.

There are two ways to address this. Either the deserializer must be configured to ignore casing, or the API must be changed to not camel-case.

System.Text.Json's JsonSerializerOptions Class has several options for configuring on the API side. For specific directions on how to set up in a .NET Core 5.0 Web API project, ref here.