8

I have a string of JSON and the keys have uppercase and lowercase characters:

{"employees":[
    {"FIrstName":"John", "LASTname":"Doe"},
    {"FIRSTNAME":"Anna", "LaSTNaME":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

I want convert it to a JToken object and have all the keys in the JToken be lowercase. So internally in the JToken it should be as follows:

{"employees":[
    {"firstname":"John", "lastname":"Doe"},
    {"firstname":"Anna", "lastname":"Smith"},
    {"firstname":"Peter", "lastname":"Jones"} 
]}

Previously I was using JToken json = JToken.Parse(jsonString); to convert, but I can't find out how to make the keys lowercase. Any ideas?

The reason why I need to do this is so that my JsonSchema validation will be case insensitive.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Rafi
  • 2,433
  • 1
  • 25
  • 33
  • JProperty has only getter for Name so I think you will have problems changing it. – mybirthname Nov 29 '16 at 07:23
  • True. I was thinking there was some way to customize the parsing mechanism. Similar to the js parse function that has the reviver parameter (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) to edit the parsing. I'm just not sure how to customize in json.net. – Rafi Nov 29 '16 at 07:29
  • if you want to create custom Json converter, check this out. I don't know if it will help in your current case:http://stackoverflow.com/questions/8030538/how-to-implement-custom-jsonconverter-in-json-net-to-deserialize-a-list-of-base – mybirthname Nov 29 '16 at 07:30
  • 1
    Use this, perhaps, after loading the JSON? [Json.net rename properties](https://stackoverflow.com/questions/11679804/json-net-rename-properties). – dbc Nov 29 '16 at 08:26
  • @Jim Why did you remove your answer? Although it didn't help in my situation, it could help other users. – Rafi Nov 29 '16 at 10:58
  • @Rafi I was just not sure if it was suited to leave it, since you can't use `.IsValid(sheme)` on a deserialized object :-) – Jim Nov 29 '16 at 11:04

1 Answers1

6

One possible way to solve this with minimal code is to subclass the JsonTextReader and override the Value property to return a lowercase string whenever the current TokenType is PropertyName:

public class LowerCasePropertyNameJsonReader : JsonTextReader
{
    public LowerCasePropertyNameJsonReader(TextReader textReader)
        : base(textReader)
    {
    }

    public override object Value
    {
        get
        {
            if (TokenType == JsonToken.PropertyName)
                return ((string)base.Value).ToLower();

            return base.Value;
        }
    }
}

This works because the underlying JsonTextReader keeps the TokenType updated as its internal state changes, and the serializer (actually the JsonSerializerInternalReader class) relies on that when it goes to retrieve the property name from the reader via the Value property.

You can create a short helper method to make it easy to deserialize using the custom reader:

public static class JsonHelper
{
    public static JToken DeserializeWithLowerCasePropertyNames(string json)
    {
        using (TextReader textReader = new StringReader(json))
        using (JsonReader jsonReader = new LowerCasePropertyNameJsonReader(textReader))
        {
            JsonSerializer ser = new JsonSerializer();
            return ser.Deserialize<JToken>(jsonReader);
        }
    }
}

Then in your code, just replace this:

JToken json = JToken.Parse(jsonString);

with this:

JToken json = JsonHelper.DeserializeWithLowerCasePropertyNames(jsonString);

Fiddle: https://dotnetfiddle.net/A0S3I1

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • This is exactly the solution I was looking for. It changes to lowercase on parsing which is the most effecient. – Rafi Nov 30 '16 at 10:25
  • I'd recommend using `ToLowerInvariant()` rather than `ToLower()` to avoid inconsistencies across locales e.g. with the [turkish I](http://www.i18nguy.com/unicode/turkish-i18n.html). – dbc May 23 '22 at 16:56