using System.Text.RegularExpressions;
public static string ToJsonIdentifier(string s)
if (string.IsNullOrEmpty(s)) return s;
if (string.IsNullOrWhiteSpace(s)) return String.Empty;
s = Regex.Replace(s, @"\s+", "");
if (!string.IsNullOrEmpty(s) && s.Length == 1)
return s.ToLowerInvariant();
var x = s.Replace("_", "");
if (x.Length == 0) return string.Empty;
x = Regex.Replace(x, "([A-Z])([A-Z]+)($|[A-Z])",
m => m.Groups[1].Value + m.Groups[2].Value.ToLower() + m.Groups[3].Value);
return char.ToLower(x[0]) + x.Substring(1);
public static void Main()
Console.WriteLine(ToJsonIdentifier("Hello World This is me"));
Console.WriteLine(ToJsonIdentifier("HelloWorld"));
Console.WriteLine(ToJsonIdentifier("Hello123"));
Console.WriteLine(ToJsonIdentifier("myFriendly"));
Console.WriteLine(ToJsonIdentifier("Customer_,ID"));
Console.WriteLine(ToJsonIdentifier("customerId"));