Search code examples
jsonasp.net-web-apidatamember

How do I override a name in JSON returned by ASP.NET


I have the following code in an ASP.NET Web API 2 app:

[DataMember(Name = "override")]
public bool? _override;

But the JSON I receive has that member named _override, not override. How can I change the naming in the JSON?


Solution

  • As asp.Net web API 2 uses Json.NET internally for json serialization/deserialization,

    JsonProperty attribute can be used to override property name on serialization.

    so [JsonProperty(PropertyName = "override")] should do the trick.

    Thanks.