Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change all keys to lowercase when parsing JSON to a JToken

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.

like image 373
Rafi Avatar asked Nov 29 '16 07:11

Rafi


1 Answers

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

like image 80
Brian Rogers Avatar answered Oct 03 '22 22:10

Brian Rogers