Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get a friendly attribute name in API payload response?

I want the get request to return a friendly names instead of the actual db column names. Is it possible?

my model class

public class Employee
    {
        public int EmployeeID { get; set; } 

        [DisplayName("First Name")]
        public string FirstName { get; set; } = string.Empty;

        public string LastName { get; set; } = string.Empty;

        public string Phone { get; set; } = string.Empty;

        public string Email { get; set; } = string.Empty;
}

payload that is generated. Note that the First Name is not changed

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "employeeID": 1501,
  "firstName": "Syed Omar",
  "lastName": "Khan",
  "phone": "9234567891",
  "email": "Syed Omar@yahoo.com",
}

>Solution :

If you want to change json property name of serialized class you can use corresponding attribute depending on json library used. For System.Text.Json it would be JsonPropertyNameAttribute. For Newtonsoft’s Json.NET – JsonPropertyAttribute:

[JsonPropertyName("First Name")]
public string FirstName { get; set; } = string.Empty;

Or

[JsonProperty("First Name")]
public string FirstName { get; set; } = string.Empty;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading