It would be easier if the data model or query result returning function gives you the data as such.
If that’s not the case, it would take multifold approach to do what you’re asking:
One example way of doing it in a single line would be like this:
{oldVal, NewVal} = Kernel.get_and_update_in(query_result, ["blog", "posts"], &{&1, hd(&1) |> Enum.flat_map(fn({k, v}) -> Enum.map(v, fn x -> %{k => x} end) end)})
{[%{"val" => ["23gh", "56kh", "97mh"]}],
%{
"blog" => %{
"posts" => [%{"val" => "23gh"}, %{"val" => "56kh"}, %{"val" => "97mh"}]
}
}}
Other sane way would be to extract the val list:
valMap = Kernel.get_in(query_result, ["blog", "posts"]) |> List.first()
valList = valMap |> Map.get("val")
changedValList = valList |> Enum.map(&(%{"val" => &1}))
result = query_result |> Kernel.put_in(["blog", "posts"], changedValList)
%{
"blog" => %{
"posts" => [%{"val" => "23gh"}, %{"val" => "56kh"}, %{"val" => "97mh"}]
}
}
Both are not the most optimized ways, if it has to be done recursively, so it’s better to get the data sorted at the source level, if possible.






















