How to remove k__BackingField from json when Deserialize
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The k__BackingField prefix appears in JSON output when serializing C# classes that use the [Serializable] attribute with a serializer that reads private fields instead of public properties. The compiler generates hidden backing fields like <Name>k__BackingField for auto-properties, and certain serializers expose them. The fix depends on which serializer you use: remove [Serializable], add [DataContract]/[DataMember] attributes, or configure the serializer to use property names.
Why It Happens
When you declare an auto-property in C#:
The compiler generates hidden backing fields:
When a serializer like BinaryFormatter or older JsonConvert settings reads fields (not properties), the JSON output looks like:
Fix 1: Remove [Serializable] (Simplest)
If you are not using binary serialization, simply remove the [Serializable] attribute:
Fix 2: Use [DataContract] and [DataMember]
If you need [Serializable] for other reasons, use [DataContract] to control JSON property names:
[DataContract] tells the serializer to use the property names (or custom names via [DataMember(Name = "...")]) instead of backing field names.
Fix 3: Newtonsoft.Json (Json.NET)
With Newtonsoft.Json, configure the contract resolver to ignore backing fields:
Or use CamelCasePropertyNamesContractResolver for camelCase output:
You can also decorate properties with [JsonProperty]:
Fix 4: System.Text.Json (.NET 6+)
System.Text.Json serializes public properties by default and does not expose backing fields:
System.Text.Json ignores fields by default. To include fields, you must explicitly opt in with JsonSerializerOptions.IncludeFields = true.
Fix 5: Custom Contract Resolver (Advanced)
If you need [Serializable] and cannot add [DataContract], create a custom resolver that filters out backing fields:
Common Pitfalls
- Assuming
[Serializable]is needed for JSON:[Serializable]is for binary serialization (BinaryFormatter). JSON serializers likeSystem.Text.Jsonand Newtonsoft.Json do not require it. Remove it unless you specifically need binary serialization. - Using
BinaryFormatterfor JSON:BinaryFormatteris insecure and deprecated in .NET 8+. If you seek__BackingFieldin your output, you may be using the wrong serializer entirely. Switch toSystem.Text.Jsonor Newtonsoft.Json. - Mixing
[DataContract]and[JsonProperty]: If both attributes are present, the serializer may use one and ignore the other depending on configuration. Pick one approach and use it consistently across your model classes. - Forgetting
[DataMember]on all properties: When[DataContract]is applied, only properties marked with[DataMember]are serialized. Unmarked properties are silently excluded from the output. - Deserialization fails with old JSON: If existing JSON contains
k__BackingFieldkeys and you fix the serialization, deserialization of the old data breaks. Map old field names during migration using[JsonProperty("<Name>k__BackingField")]temporarily.
Summary
k__BackingFieldappears when serializers read compiler-generated backing fields instead of public properties- The simplest fix is to remove
[Serializable]if binary serialization is not needed - Use
[DataContract]/[DataMember]to control serialized property names when[Serializable]is required System.Text.Jsonignores backing fields by default — it does not have this problem- Newtonsoft.Json with
DefaultContractResolveralso serializes properties correctly - Never use
BinaryFormatterfor JSON serialization — it is deprecated and insecure
Related reading
- How to remove the focus from a TextBox in WinForms?
- How to rename a database column in Entity Framework 5 Code First migrations without losing data?
- How to represent a C property in UML?
- How to resolve NuGet dependency hell
- How to rethrow InnerException without losing stack trace in C?
- How to retrieve actual item from HashSetT?
- How to retrieve the LoaderExceptions property?
- How to return a string from async

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.