I would suggest restoring your JSON with renamed properties. I donβt think you should worry about speeding fines as this is usually not a problem. Here is how you can do it.
public static JToken Rename(JToken json, Dictionary<string, string> map) { return Rename(json, name => map.ContainsKey(name) ? map[name] : name); } public static JToken Rename(JToken json, Func<string, string> map) { JProperty prop = json as JProperty; if (prop != null) { return new JProperty(map(prop.Name), Rename(prop.Value, map)); } JArray arr = json as JArray; if (arr != null) { var cont = arr.Select(el => Rename(el, map)); return new JArray(cont); } JObject o = json as JObject; if (o != null) { var cont = o.Properties().Select(el => Rename(el, map)); return new JObject(cont); } return json; }
And here is a usage example:
var s = @"{ ""A"": { ""B"": 1, ""Test"": ""123"", ""C"": { ""Test"": [ ""1"", ""2"", ""3"" ] } } }"; var json = JObject.Parse(s); var renamed = Rename(json, name => name == "Test" ? "TestRenamed" : name); renamed.ToString().Dump();
Dmitry Lobanov
source share