I am looking to expand this post, How can a key be renamed in an Association?.
I would like a function that automatically replaces any and all keys with the rule.
Here is what I have so far
KeyReplace[data_, rule_] := KeyMap[Replace[#, rule] &, data];
KeyReplace[data_, {rules__}] := KeyMap[Replace[#, {rules}] &, data];
KeyReplace[data_List, rule_] := KeyReplace[#, rule] & /@ data;
KeyReplace[data_List, {rules__}] := KeyReplace[#, {rules}] & /@ data;
KeyReplace[data_Association, rule_, depth_: 1] := MapAt[KeyReplace[#, rule] &, data, {depth, All}];
KeyReplace[data_Association, {rules__}, depth_: 1] := MapAll[KeyReplace[#, rules] &, data, {depth, All}];
test=<|"key1" -> <|"val11" -> val11, "val12" -> val12|>, "key2" -> {<|"val21" -> val21, "val22" -> val22|>, <|"val31" -> val31, "val32" -> val32|>}|>;
This works for the everything except the "val11" and "val12" keys.
The function should just take in any data structure and then replace the keys that match the pattern.
KeyReplace[test,"vall11"-> "new"]
(*<|"key1" -> <|"new" -> val11, "val12" -> val12|>, "key2" -> {<|"test" -> val21, "val22" -> val22|>, <|"val31" -> val31, "val32" -> val32|>}|>*)
KeyReplace[test,{"vall11"-> "new","val32"-> "new2"}]
(*<|"key1" -> <|"new" -> val11, "val12" -> val12|>, "key2" -> {<|"test" -> val21, "val22" -> val22|>, <|"val31" -> val31, "new2" -> val32|>}|>*)
KeyReplace[test,"val32"-> "new2"]
(*<|"key1" -> <|"vall11" -> val11, "val12" -> val12|>, "key2" -> {<|"test" -> val21, "val22" -> val22|>, <|"val31" -> val31, "new2" -> val32|>}|>*)
This is as close as I could get but could not get the first association to replace keys.