Considering an association:
<|"a" -> <|aa-> "asc", bb-> "asd", cc-> 0, ImageType -> "asd", dd-> "asd"|>|>
How can I make the inner keys strings?
<|"a" -> <|"aa"-> "asc", "bb"-> "asd", "cc"-> 0, "ImageType" -> "asd", "dd"-> "asd"|>|>
Answer
assoc= <|"a" -> <| aa-> "asc", bb->"asd", cc->0, ImageType->"asd", dd-> "asd"|>|>;
KeyMap[ToString]/@assoc
(* <|"a" -> <|"aa" -> "asc", "bb" -> "asd", "cc" -> 0,
"ImageType" -> "asd", "dd" -> "asd"|>|> *)
Update:
but what if I have n levels?
I hope there is a better/cleaner way to deal with nested associations than the following:
assoc2= <| a-> <| aa-> "asc", bb->"asd", cc->0, ImageType->"asd",
dd-> <|dd1->"asd", dd2->"asd2"|>|>|>;
Replace[assoc2/. Association->foo, Rule[a_,b_]:>Rule[ToString[a],b],
{0,Infinity}]/. foo->Association
(* <|"a" -> <|"aa" -> "asc", "bb" -> "asd", "cc" -> 0,
"ImageType" -> "asd",
"dd" -> <|"dd1" -> "asd", "dd2" -> "asd2"|>|>|> *)
Comments
Post a Comment