Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6012205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:22:12+00:00 2026-05-23T02:22:12+00:00

Is there simple way using JSON in .NET to ensure that the keys are

  • 0

Is there simple way using JSON in .NET to ensure that the keys are sent as lower case?

At the moment I’m using the newtonsoft’s Json.NET library and simply using

string loginRequest = JsonConvert.SerializeObject(auth);

In this case auth is just the following object

public class Authority
{
    public string Username { get; set; }
    public string ApiToken { get; set; }
}

This results in

{"Username":"Mark","ApiToken":"xyzABC1234"}

Is there a way to ensure that the username and apitoken keys come through as lowercase?

I don’t want to simply run it through String.ToLower() of course because the values for username and apitoken are mixed case.

I realise I can programatically do this and create the JSON string manually, but I need this for approx 20 or so JSON data strings and I’m seeing if I can save myself some time. I’m wondering if there are any already built libraries that allow you to enforce lowercase for key creation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:22:13+00:00Added an answer on May 23, 2026 at 2:22 am

    You can create a custom contract resolver for this. The following contract resolver will convert all keys to lowercase:

    public class LowercaseContractResolver : DefaultContractResolver
    {
        protected override string ResolvePropertyName(string propertyName)
        {
            return propertyName.ToLower();
        }
    }
    

    Usage:

    var settings = new JsonSerializerSettings();
    settings.ContractResolver = new LowercaseContractResolver();
    var json = JsonConvert.SerializeObject(authority, Formatting.Indented, settings);
    

    Wil result in:

    {"username":"Mark","apitoken":"xyzABC1234"}
    

    If you always want to serialize using the LowercaseContractResolver, consider wrapping it in a class to avoid repeating yourself:

    public class LowercaseJsonSerializer
    {
        private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            ContractResolver = new LowercaseContractResolver()
        };
    
        public static string SerializeObject(object o)
        {
            return JsonConvert.SerializeObject(o, Formatting.Indented, Settings);
        }
    
        public class LowercaseContractResolver : DefaultContractResolver
        {
            protected override string ResolvePropertyName(string propertyName)
            {
                return propertyName.ToLower();
            }
        }
    }
    

    Which can be used like this:

    var json = LowercaseJsonSerializer.SerializeObject(new { Foo = "bar" });
    // { "foo": "bar" }
    

    ASP.NET MVC4 / WebAPI

    If you are using ASP.NET MVC4 / WebAPI, you can use a CamelCasePropertyNamesContractResolver from Newtonsoft.Json library which included by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to deserialize a simple JSON string into a .NET
Is there a simple way, using C#, to open an arbitrary URL, read in
Using JQuery, is there a simple way to select the text immediately after a
Using unmanaged C++ on a Windows platform, is there a simple way to detect
I have a simple question. Is there a way ( using reflections I suppose
Is there a way to use simple sql queries on ASP MVC without using
Is there a simple way to cache MySQL queries in PHP or failing that,
Is there a simple way in .NET to quickly get the current protocol, host,
Is there any simple way to programatically colorize images in .NET? Basically we have
Simply put, is there a way to create a 2D javascript array using similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.