iamacube

iamacube

Hi! I have a list of maps and I want to rename the keys.

What I have:

[info] entities: [ok: %{"Email Address [Required]" => "fblue@mikasa.com", "First Name [Required]" => "Blue", "Last Name [Required]" => "First"}, ok: %{"Email Address [Required]" => "sred@mikasa.com", "First Name [Required]" => "Red", "Last Name [Required]" => "Second"}, ok: %{"Email Address [Required]" => "tyellow@mikasa.com", "First Name [Required]" => "Yellow", "Last Name [Required]" => "Third"}]

What I want to achieve:

[info] entities: [ok: %{"email" => "fblue@mikasa.com", "first_name" => "Blue", "last_name" => "First"}, ok: %{"email" => "sred@mikasa.com", "first_name" => "Red", "last_name" => "Second"}, ok: %{"email" => "tyellow@mikasa.com", "first_name" => "Yellow", "last_name" => "Third"}]

First Name [Required] to first_name
Last Name [Required] to last_name
Email Address [Required] to email

I am not sure how will I work on the keys with all the white space and the [] brackets.

Showing Posts 1 to 6

kokolegorille

kokolegorille

You might remove [Required] with String.replace + trim. You might even transform “LastName” to “last_name” with Macro.underscore.

But how do You want him to translate “Email Address” to “email”?

Unless You use some custom rules…

msimonborg

msimonborg

for "Last Name [Required]""last_name"

key |> String.trim(" [Required]") |> String.downcase() |> String.replace(" ", "_")

for "Email Address [Required]""email", you might just pattern match on the special case in a multiple function clause

defp rename_key({"Email Address [Required]", val}), do: {"email", val}
defp rename_key({key, val}) do
  # operate on the key
  {new_key, val}
end
Eiji

Eiji

Here is a complete code:

defmodule Example do
  # First of all we call map on our keyword
  def sample(keyword) when is_list(keyword), do: Enum.map(keyword, &rename/1)

  # When key is ok atom and value is a map call rename function
  defp rename({key = :ok, map}) when is_map(map), do: {key, rename(map)}

  # Rename for maps is using for comprehension changing only key and keeping value as is
  defp rename(map) when is_map(map) do
    for {key, value} <- map, into: %{}, do: {rename(key), value}
  end

  # Rename for binary is removing trailing string and calling rename_key
  defp rename(binary) when is_binary(binary) do
    binary |> String.trim_trailing(" [Required]") |> rename_key()
  end

  # A simple pattern-match in rename_key for a special email case
  defp rename_key("Email Address"), do: "email"

  # rename_key for any other string
  defp rename_key(key) when is_binary(key) do
    key |> String.replace(" ", "") |> Macro.underscore()
  end
end

expected_output = [
  ok: %{
    "email" => "fblue@mikasa.com",
    "first_name" => "Blue",
    "last_name" => "First"
  },
  ok: %{
    "email" => "sred@mikasa.com",
    "first_name" => "Red",
    "last_name" => "Second"
  },
  ok: %{
    "email" => "tyellow@mikasa.com",
    "first_name" => "Yellow",
    "last_name" => "Third"
  }
]

input = [
  ok: %{
    "Email Address [Required]" => "fblue@mikasa.com",
    "First Name [Required]" => "Blue",
    "Last Name [Required]" => "First"
  },
  ok: %{
    "Email Address [Required]" => "sred@mikasa.com",
    "First Name [Required]" => "Red",
    "Last Name [Required]" => "Second"
  },
  ok: %{
    "Email Address [Required]" => "tyellow@mikasa.com",
    "First Name [Required]" => "Yellow",
    "Last Name [Required]" => "Third"
  }
]

result = Example.sample(input)
IO.puts(result == expected_output)
# true

Helpful resources:

  1. Kernel.is_list/1
  2. Kernel.is_map/1
  3. Enum.map/2
  4. Kernel.SpecialForms.for/1
  5. String.replace/4
  6. String.trim_trailing/2
  7. Macro.underscore/1
  8. Patterns and Guards
al2o3cr

al2o3cr

The simplest way to accomplish what you’ve written is to look up replacement keys in a supplied map. For instance:

defmodule KeyRenamer do
  @key_replacements %{
    "First Name [Required]" => "first_name",
    "Last Name [Required]" => "last_name",
    "Email Address [Required]" => "email"
  }

  def rename_keys(map) do
    Map.new(map, fn k, v ->
      new_key = Map.get(@key_replacements, k, k)
      {new_key, v}
    end
  end
end

An alternative approach would be to fix the code that’s generating these keys - for instance, if it’s extracting a value from an HTML label maybe it should be using the input’s name or something instead…

sabiwara

sabiwara

Elixir Core Team

If the keys are known and static, and you don’t need to make this logic reusable, you could also use plain pattern-matching:

  def rename_keys(%{
    "First Name [Required]" => first_name,
    "Last Name [Required]" => last_name,
    "Email Address [Required]" => email
  }) do
    %{first_name: first_name, last_name: last_name, email: email}
  end
blackham

blackham

I don’t mind being that guy that comments on OLD threads. I ran into a sexy way to rename some keys (or all if you want). I tried googling for it later and couldn’t find it. Google just keep sending me back to this thread. I eventually found the solution in some old code. So, I’m going to dump this solution here so next time I’ll find it faster. (and maybe it will help someone else)

To rename SOME of the keys:

bad_data = %{
  "lat" => 12.43244,
  long: -5.342,
  name: "something",
  age: 91
}

good_data = Map.new(bad_data, fn
  {"lat", lat} -> {"latitude", lat}
  {:long, long} -> {:longitude, long}
  anything -> anything
end)

Results:

%{
:age => 91,
:longitude => -5.342,
:name => "something",
"latitude" => 12.43244
}

I mixed :keys and “keys” in the example to show it works with both.

good_data should now have the keys latitude, longitude, name, age. It might be cool if Hexdoc had a section showing some common uses that aren’t functions, but indexed like functions. Example “rename_key()” Don’t need the function because you can easily do it like (copy and paste example above)

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews