Header Ads

How to Configure JSON Format with Camel-Case Notation in ASP.NET MVC5

Whenever the word JSON is heard, the first thing that comes in mind is Restful web API. JSON is the most popular choice and commonly used data exchange format between server-side and client-side. From complex to simple, JSON can easily be adopted for accessing and manipulating data at both server-side and client-side. As simple as JSON is sounds, there are configurations needed within certain platforms to properly ensure JSON format.

Today, I shall be demonstrating configuration of JSON format with camel case notation and removing extra slashes using ASP.NET REST Web API platform.


Prerequisites:

Following are some prerequisites before you proceed any further in this tutorial.
  1. Knowledge of Camel Case Notation.
  2. Knowledge of Pascal Case Notation. 
  3. Knowledge of REST Web API.
  4. Knowledge of ASP.NET MVC5.
  5. Knowledge of C# Programming.
The example code is being developed in Microsoft Visual Studio 2019 Professional.

Download Now!

Let's begin now.

1) Create a new Web API project and name it "WebApiJsonFormat".  

2) Install Json.NET - Newtonsoft via Nuget Package Manager.
3) Now, create your .NET JSON object mappers based on your JSON data either manually or via Json2Csharp online tool. I am using following JSON sample with .NET object mapper i.e.

Sample JSON Data

{
	"fullname": "John Adam",
	"age": 30,
	"CarInfo": "I have big car"
}


C#.NET Json Object Mapper

... 

public class RequestObj
{
    // Gets or sets full name property.
    public string FullName { get; set; }

    // Gets or sets age property.
    public int Age { get; set; }

    // Gets or sets car info property.
    public string CarInfo { get; set; }
}

...

Notice in the above sample JSON that property/key names do not have proper naming convention i.e. either camel case or pascal case notation. On the other hand the C#.NET object mapper class, the properties are in pascal notation i.e. first letter is capital, but, as we create our JSON response our requirements needed responding JSON in Camel case notation.

4) In next step create your web API method either POST or GET, I am making a simple HTTP POST method which after de-serializing the input JSON, serializes it back and send response.

5) So, before making any JSON case format or extra slashes removal configuration, execute the project and test your web API method response format as shown below i.e.

In the above snippet you can clearly see that my response JSON is in pascal case notation similar to my JSON object mapper and there are many extra slashes are coming. Know that having extra slashes in JSON is not a proper JSON format as it might cause errors in underlying access integrating client platform especially if the client platform is not .NET based. As I have already established the understanding that RESTful web API is client platform independent, therefore, my JSON response from ASP.NET MVC project is not according to the standards as it is making JSON .NET platform dependent. So, I need to configure this issue in my .NET project and ensure that my response JSON is according to the followed standards.
 
6) To fix extra slashes issues, in my web API method whether GET or POST I have created a response using HttpResponseMessage class instead of a string class as follow i.e.

...

public HttpResponseMessage Post([FromBody] JToken postData)
{
    // Initialization
    string jsonData = string.Empty;
    
    ...

    // Process code logic to create JSON response data.

    ...

    // Creating HTTP Response Message.
    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");

    // Info.
    return response;
}

...

7) Now upon executing the project you will see that the extra slashes issue has been removed and my response JSON is now in standard JSON format as shown below i.e.

In the above snippet notice that my response JSON is still in pascal case notation, but, according to my requirements I need my response JSON in camel case notation, therefore, I need to fix that issue a well 

8) To fix camel case notation issue in my response JSON data, add following lines of code at the end of "Register(...)" method in "App_Start ->WebApiConfig.cs" file  i.e.

...

// If using JsonConvert for serialization.
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

...

The above lines of code will be effective only whenever I use JsonConvert to serialize my JSON data object.

9) Now, upon executing the project, you can see that now my response JSON object in standard format with camel case notation according my business requirements i.e.

Conclusion

In this article, you will learn abput the configuration of JSON format with camel case notation and removing extra slashes using ASP.NET REST Web API platform. You will learn about JSON object mapper along with how to generate it using online tools or manually. You will also learn about how to fix extra slashes issue and configure response JSON data to camel notation. Finally, you will learn to create HTTP response message along with standardizing the response JSON according general standards.

Video Demo


5 comments:

  1. Much thanks to you for your blog article.Really anticipating read more. Will peruse on…
    Amazid Shop

    ReplyDelete
  2. Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon. Best Water Treatment Companies in UAE
    anti hair fall shower filter
    Sewage water treatment plant in Dubai UAE

    ReplyDelete
  3. Interesting subject matter for a blog. I have been searching the Internet for amusing and came upon your internet site. Fabulous submit. Thanks a ton for sharing your understanding! It is outstanding to peer that some people nevertheless put in an effort into dealing with their websites. Hot Water Repairs

    ReplyDelete