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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
for
"Last Name [Required]"→"last_name"for
"Email Address [Required]"→"email", you might just pattern match on the special case in a multiple function clauseEiji
Here is a complete code:
Helpful resources:
al2o3cr
The simplest way to accomplish what you’ve written is to look up replacement keys in a supplied map. For instance:
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
labelmaybe it should be using theinput’s name or something instead…sabiwara
If the keys are known and static, and you don’t need to make this logic reusable, you could also use plain pattern-matching:
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:
Results:
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)