koolquark
How to convert nested property list ( having string keys ) to Elixir nested map?
Hi, how to convert a list like this to nested map in elixir ?
[
{"_id", "idhere"},
{"App-Version", "version_here"},
{"TX-ID", "tx-id-here"},
{"Method", "method-here"},
{"Body",
{[
{"Identification",
[
{"A", "A-Val"},
{"B", "B-Val"},
{"C", "C-Val"},
{"D", "D-Val"}
]},
{"CurrentTime", "2023-02-23T16:40:13+05:30"},
{"Names", ["Name1", "Name2"]}
]}}
]
Marked As Solved
andreaseriksson
I would do it something like this:
def transform([{_, _}|_] = list) do
Enum.reduce(list, %{}, fn {key, val}, map ->
Map.put(map, key, transform(val))
end)
end
def transform({[{_, _}|_] = list}), do: transform(list)
def transform(val), do: val
And then call it with just
transform/1
And the result would be something like:
%{
"App-Version" => "version_here",
"Body" => %{
"CurrentTime" => "2023-02-23T16:40:13+05:30",
"Identification" => %{
"A" => "A-Val",
"B" => "B-Val",
"C" => "C-Val",
"D" => "D-Val"
},
"Names" => ["Name1", "Name2"]
},
"Method" => "method-here",
"TX-ID" => "tx-id-here",
"_id" => "idhere"
}
3
Also Liked
hlx
May I offer an alternative:
def transform([_ | _] = list) do
Map.new(list, fn
{k, [{_, _} | _] = v} -> {k, transform(v)}
{k, {[_ | _] = v}} -> {k, transform(v)}
{k, v} -> {k, v}
end)
end
4
koolquark
Map.new with fn method is more concise . Thanks
I tried this initially, but didn’t complete successfully.
1
Popular in Questions
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Background
Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
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
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New







