What is the way to convert %{\"foo\" => \"bar\"}
to %{foo: \"bar\"}
in Elixir?
There's a library for this, https://hex.pm/packages/morphix. It also has a recursive function for embedded keys.
Most of the work is done in this function:
defp atomog (map) do
atomkeys = fn({k, v}, acc) ->
Map.put_new(acc, atomize_binary(k), v)
end
Enum.reduce(map, %{}, atomkeys)
end
defp atomize_binary(value) do
if is_binary(value), do: String.to_atom(value), else: value
end
Which is called recursively. After reading @Galzer's answer I'll probably convert this to use String.to_existing_atom
soon.