Home > Enterprise >  How to convert json value to lowercase - Deserialization
How to convert json value to lowercase - Deserialization

Time:06-15

Please need some help.

I am trying to convert all json value (not the key) to lowercase. I browsed and found only how to convert property name (key), not property value.

I spent the whole day trying to figure out how to do it.

To have somthing like

"name": "luke skywalker", 
"hair_color": "blond", 
"skin_color": "fair",

Below is the code I'm trying so far. It doesn't work.

private static void ChangePropertiesToLowerCase(JObject jsonObject)
        {
            foreach (var property in jsonObject.Properties().ToList())
            {
                if (property.Value.Type == JTokenType.Object)// replace property names in child object
                    ChangePropertiesToLowerCase((JObject)property.Value);

                property.Replace(new JProperty(property.Name.ToLower(), property.Value));// properties are read-only, so we have to replace them
            }
        }

Below is the json file

{
    "count": 82, 
    "next": "https://swapi.dev/api/people/?page=2", 
    "previous": null, 
    "results": [
        {
            "name": "Luke Skywalker", 
            "height": "172", 
            "mass": "77", 
            "hair_color": "blond", 
            "skin_color": "fair", 
            "eye_color": "blue", 
            "birth_year": "19BBY", 
            "gender": "male", 
            "homeworld": "https://swapi.dev/api/planets/1/", 
            "films": [
                "https://swapi.dev/api/films/1/", 
                "https://swapi.dev/api/films/2/", 
                "https://swapi.dev/api/films/3/", 
                "https://swapi.dev/api/films/6/"
            ], 
            "species": [], 
            "vehicles": [
                "https://swapi.dev/api/vehicles/14/", 
                "https://swapi.dev/api/vehicles/30/"
            ], 
            "starships": [
                "https://swapi.dev/api/starships/12/", 
                "https://swapi.dev/api/starships/22/"
            ], 
            "created": "2014-12-09T13:50:51.644000Z", 
            "edited": "2014-12-20T21:17:56.891000Z", 
            "url": "https://swapi.dev/api/people/1/"
        }, 
        {
            "name": "C-3PO", 
            "height": "167", 
            "mass": "75", 
            "hair_color": "n/a", 
            "skin_color": "gold", 
            "eye_color": "yellow", 
            "birth_year": "112BBY", 
            "gender": "n/a", 
            "homeworld": "https://swapi.dev/api/planets/1/", 
            "films": [
                "https://swapi.dev/api/films/1/", 
                "https://swapi.dev/api/films/2/", 
                "https://swapi.dev/api/films/3/", 
                "https://swapi.dev/api/films/4/", 
                "https://swapi.dev/api/films/5/", 
                "https://swapi.dev/api/films/6/"
            ], 
            "species": [
                "https://swapi.dev/api/species/2/"
            ], 
            "vehicles": [], 
            "starships": [], 
            "created": "2014-12-10T15:10:51.357000Z", 
            "edited": "2014-12-20T21:17:50.309000Z", 
            "url": "https://swapi.dev/api/people/2/"
        }, 
        
        }
    ]
}

CodePudding user response:

You can use JsonConverter to solve your problem.

You can create your own JsonConverter to handle string value. In the following code I created this customer JsonConverter to convert string values to lower case while writing to JSON.

public class StringConverter : JsonConverter<string>
{
    public override void WriteJson(JsonWriter writer, string value, JsonSerializer serializer)
    {
        // converting to lower case string while writing to JSON outcome.
        writer.WriteValue(value.ToLower());
    }

    public override string ReadJson(JsonReader reader, Type objectType, string existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        // returning the value without change while deserializing.
        return (string)reader.Value;
    }
}

And following is the way you can use the above converter.

// let say your class name is SearchResult;
// JSON string is stored in jsonString variable.


var searchResult = JsonConvert.Deserialize<SearchResult>(jsonString);

var jsonStringLowerCase = JsonConvert.Serialize(jsonString, new StringConverter());

With above code jsonStringLowerCase will have JSON with all the string property values converted to lower case.

I hope this will help you solve your issue.

  • Related