How to remove k__BackingField from json when Deserialize
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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

