4

I'm having a JSON string, it has Key in the form of Camel-case but I need to convert the Key to Pascal-case.

Actual JSON String

string jsonString = "{\"personName\":{\"firstName\":\"Emma\",\"lastName\":\"Watson\"}}";

Expected JSON String : Needs to convert from the above JSON string.

string jsonString = "{\"PersonName\":{\"FirstName\":\"Emma\",\"LastName\":\"Watson\"}}";

Kindly assist me how to convert this using C#.

David A
  • 379
  • 1
  • 5
  • 15
  • probaby this link could help you: https://stackoverflow.com/questions/9247478/pascal-case-dynamic-properties-with-json-net – Kien Chu Jun 13 '17 at 02:01
  • A variant of this: [C# Capitalizing string, but only after certain punctuation marks](https://stackoverflow.com/questions/5664286/c-sharp-capitalizing-string-but-only-after-certain-punctuation-marks) – Theraot Jun 13 '17 at 02:02
  • @kienct89 - I will try... –  Jun 13 '17 at 02:05
  • How is this different from your very similar question 12 hours ago? https://stackoverflow.com/questions/44500707/json-string-camel-case-to-pascal-case-deserializeobject-issue-in-c-sharp – pcdev Jun 13 '17 at 02:23

1 Answers1

6

Because I can't sleep.

If you define the following static class of extension methods...

public static class JsonExtensions
{
    public static void Capitalize(this JArray jArr)
    {
        foreach(var x in jArr.Cast<JToken>().ToList())
        {
            var childObj = x as JObject;
            if(childObj != null)
            {
                childObj.Capitalize();
                continue;
            }
            var childArr = x as JArray;
            if(childArr != null)
            {
                childArr.Capitalize();
                continue;
            }
        }
    }

    public static void Capitalize(this JObject jObj)
    {
        foreach(var kvp in jObj.Cast<KeyValuePair<string,JToken>>().ToList())
        {
            jObj.Remove(kvp.Key);
            var newKey = kvp.Key.Capitalize();
            var childObj = kvp.Value as JObject;
            if(childObj != null)
            {
                childObj.Capitalize();
                jObj.Add(newKey, childObj);
                return;
            }
            var childArr = kvp.Value as JArray;
            if(childArr != null)
            {
                childArr.Capitalize();
                jObj.Add(newKey, childArr);
                return;
            }
            jObj.Add(newKey, kvp.Value);
        }
    }

    public static string Capitalize(this string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            throw new ArgumentException("empty string");
        }
        char[] arr = str.ToCharArray();
        arr[0] = char.ToUpper(arr[0]);
        return new string(arr);
    }
}

You can:

void Main()
{
    string jsonString = 
        "{\"personName\":{\"firstName\":\"Emma\",\"lastName\":\"Watson\"}}";
    var jObj = JObject.Parse(jsonString);
    jObj.Capitalize();
    Console.WriteLine(jObj.ToString()); //yay!
}
spender
  • 117,338
  • 33
  • 229
  • 351
  • 1
    Tried to edit the code in order to fix a small bug - the return in the collection iteration would prevent other items to be iterated and capitalize, so that should be a `continue` instead. – eyal Nov 20 '19 at 13:42
  • @eyal When I get a moment, I'll review – spender Nov 20 '19 at 16:23