How can I return camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods?

Cover Image for How can I return camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🐫 Returning camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods

If you're using ASP.NET MVC and want to return JSON data in camelCase format rather than the default PascalCase, this guide will help you solve this problem using JSON.NET.

The Issue

By default, when returning JSON data from an MVC controller using the ActionResult class, the JSON data is serialized in PascalCase format. However, you want to serialize it in camelCase format.

The Solution

To return camelCase JSON serialized by JSON.NET, you need to customize the serialization settings of your ASP.NET MVC application.

Step 1: Install JSON.NET

If you haven't already, you need to install JSON.NET, a popular high-performance JSON framework for .NET. You can do this by downloading the package or by using NuGet Package Manager:

Install-Package Newtonsoft.Json

Step 2: Configure JSON.NET in your MVC application

In your ASP.NET MVC application, open the Global.asax.cs file and add the following code inside the Application_Start method:

protected void Application_Start()
{
    // Other configurations

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
        new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
}

This code sets the ContractResolver property of the JsonFormatter.SerializerSettings object to an instance of the CamelCasePropertyNamesContractResolver class. This resolver ensures that JSON object properties are serialized in camelCase format.

Step 3: Return JSON from your MVC controller

Now, you can return JSON data from your MVC controller, and it will be serialized in camelCase format.

public ActionResult GetPerson()
{
    var person = new Person
    {
        FirstName = "Joe",
        LastName = "Public"
    };

    return Json(person, JsonRequestBehavior.AllowGet);
}

Example

Using the above steps, the Person object will be serialized as:

{
  "firstName": "Joe",
  "lastName": "Public"
}

Conclusion

By configuring JSON.NET in your ASP.NET MVC application and customizing the serialization settings, you can easily return camelCase JSON from your controller methods.

Don't forget to install JSON.NET and add the necessary code in your Global.asax.cs file to enable camelCase serialization.

Now, go ahead and give it a try in your own application! If you have any questions or want to share your experiences, feel free to leave a comment below.

Happy coding! 🚀


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello