Rashmi_prueba

Rashmi_prueba

How to convert map keys from atoms to string in Elixir

What is the way to convert
[ %{id: 7, name: "A", count: 1}, %{id: 8, name: "B", count: 1}, %{id: 9, name: "C", count: 0} ]
to
[ %{"id" => "7", "name" => "A", "count" => "1"}, %{"id" => "8", "name" => "B", "count" => "1"}, %{"id" => "9", "name" => "C", "count" => "0"} ]
in Elixir?
Can any one help me?

Most Liked

hpopp

hpopp

List comprehensions are also a great choice for things like this:

list = [
  %{id: 7, name: "A", count: 1},
  %{id: 8, name: "B", count: 1},
  %{id: 9, name: "C", count: 0}
]

for elem <- list do
  for {key, val} <- elem, into: %{} do
    {to_string(key), to_string(val)}
  end
end

jaimeiniesta

jaimeiniesta

Another way to do this would be using the Jason library:

iex> data = %{:some => %{:nested => :data}}
%{some: %{nested: :data}}

iex> data |> Jason.encode!() |> Jason.decode!()
%{"some" => %{"nested" => "data"}}
NobbZ

NobbZ

@Siel gave you already an answer that works, though Map.new/2 might be more performant than Enum.map/2 |> Enum.into/2.

Also be aware that this works only for a single level of maps. If your maps are more complicated or nested, then you need to do a lot more of the heavy lifting and do some explicit recursion.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement