You can make a short helper method using Json.Net's LINQ-to-JSON API (JTokens) to accomplish this:
public static string RenameProperties(string json, Dictionary<string, string> nameMappings)
{
JContainer token = (JContainer)JToken.Parse(json);
// Note: We need to process the descendants in reverse order here
// to ensure we replace child properties before their respective parents
foreach (JProperty prop in token.Descendants().OfType<JProperty>().Reverse().ToList())
{
if (nameMappings.TryGetValue(prop.Name, out string newName))
{
prop.Replace(new JProperty(newName, prop.Value));
}
}
return token.ToString();
}
To use it, pass your JSON string and a dictionary which maps the old names to the new names.
var nameMappings = new Dictionary<string, string>()
{
{ "A", "A1" },
{ "B", "B1" },
{ "C", "C1" },
};
string modifiedJson = RenameProperties(originalJson, nameMappings);
Working demo here: https://dotnetfiddle.net/rsq5ni