How to convert map keys from strings to atoms in Elixir

前端 未结 13 1184
梦毁少年i
梦毁少年i 2021-01-31 07:07

What is the way to convert %{\"foo\" => \"bar\"} to %{foo: \"bar\"} in Elixir?

相关标签:
13条回答
  • 2021-01-31 07:29

    when you have a map inside another map

    def keys_to_atom(map) do
     Map.new(
      map,
      fn {k, v} ->
        v2 = cond do
          is_map(v) -> keys_to_atom(v)
          v in [[nil], nil] -> nil
          is_list(v) -> Enum.map(v, fn o -> keys_to_atom(o) end)
          true -> v
        end
        {String.to_atom("#{k}"), v2}
      end
     )
    end
    

    sample:

    my_map = %{"a" => "1", "b" => [%{"b1" => "1"}], "c" => %{"d" => "4"}}
    

    result

    %{a: "1", b: [%{b1: "1"}], c: %{d: "4"}}

    note: the is_list will fail when you have "b" => [1,2,3] so you can comment/remove this line if this is the case:

    # is_list(v) -> Enum.map(v, fn o -> keys_to_atom(o) end)
    
    0 讨论(0)
  • 2021-01-31 07:30

    I think the easiest way to do this is to use Map.new:

    %{"a" => 1, "b" => 2} 
    |> Map.new(fn {k, v} -> {String.to_atom(k), v} end)      
    
    => %{a: 1, b: 2}
    
    0 讨论(0)
  • 2021-01-31 07:37

    I really liked Roman Bedichevskii's answer ... but I needed something that will thoroughly atomize the keys of deeply nested yaml files. This is what I came up with:

       @doc """
       Safe version, will only atomize to an existing key
       """
       def atomize_keys(map) when is_map(map), do: Map.new(map, &atomize_keys/1)
       def atomize_keys(list) when is_list(list), do: Enum.map(list, &atomize_keys/1)
       def atomize_keys({key, val}) when is_binary(key),
         do: atomize_keys({String.to_existing_atom(key), val})
       def atomize_keys({key, val}), do: {key, atomize_keys(val)}
       def atomize_keys(term), do: term
    
       @doc """
       Unsafe version, will atomize all string keys
       """
       def unsafe_atomize_keys(map) when is_map(map), do: Map.new(map, &unsafe_atomize_keys/1)
       def unsafe_atomize_keys(list) when is_list(list), do: Enum.map(list, &unsafe_atomize_keys/1)
       def unsafe_atomize_keys({key, val}) when is_binary(key),
         do: unsafe_atomize_keys({String.to_atom(key), val})
       def unsafe_atomize_keys({key, val}), do: {key, unsafe_atomize_keys(val)}
       def unsafe_atomize_keys(term), do: term
    

    It's main limitation is that if you feed it a tuple {key, value} and the key is a binary, it will atomize it. That is something you want for keyword lists, but it is probably someone's edge case. In any case, YAML and JSON files don't have a concept of a tuple, so for processing those, it won't matter.

    0 讨论(0)
  • 2021-01-31 07:40

    First of all, @Olshansk's answer worked like a charm for me. Thank you for that.

    Next, since the initial implementation provided by @Olshansk was lacking support for list of maps, below is my code snippet extending that.

      def keys_to_atoms(string_key_map) when is_map(string_key_map) do
        for {key, val} <- string_key_map, into: %{}, do: {String.to_atom(key), keys_to_atoms(val)}
      end
    
      def keys_to_atoms(string_key_list) when is_list(string_key_list) do
        string_key_list
        |> Enum.map(&keys_to_atoms/1)
      end
    
      def keys_to_atoms(value), do: value
    

    This the sample I used, followed by the output after passing it to the above function - keys_to_atoms(attrs)

    # Input
    %{
      "school" => "School of Athens",
      "students" => [
        %{
          "name" => "Plato",
          "subjects" => [%{"name" => "Politics"}, %{"name" => "Virtues"}]
        },
        %{
          "name" => "Aristotle",
          "subjects" => [%{"name" => "Virtues"}, %{"name" => "Metaphysics"}]
        }
      ]
    }
    
    # Output
    %{
      school: "School of Athens",
      students: [
        %{name: "Plato", subjects: [%{name: "Politics"}, %{name: "Virtues"}]},
        %{name: "Aristotle", subjects: [%{name: "Virtues"}, %{name: "Metaphysics"}]}
      ]
    }
    
    

    The explanation for this is very simple. The first method is the heart of everything which is invoked for the input of the type map. The for loop destructures the attributes in key-value pairs and returns the atom representation of the key. Next, while returning the value, there are three possibilities again.

    1. The value is yet another map.
    2. The value is a list of maps.
    3. The value is none of the above, it's primitive.

    So this time, when the keys_to_atoms method is invoked while assigning value, it may invoke one of the three methods based on the type of input. The methods are organized in the snippet in a similar order.

    Hope this helps. Cheers!

    0 讨论(0)
  • 2021-01-31 07:41

    Here is what I use to recursively (1) format map keys as snakecase and (2) convert them to atoms. Keep in mind that you should never convert non-whitelisted user data to atoms as they are not garbage collected.

    defp snake_case_map(map) when is_map(map) do
      Enum.reduce(map, %{}, fn {key, value}, result ->
        Map.put(result, String.to_atom(Macro.underscore(key)), snake_case_map(value))
      end)
    end
    defp snake_case_map(list) when is_list(list), do: Enum.map(list, &snake_case_map/1)
    defp snake_case_map(value), do: value
    
    0 讨论(0)
  • 2021-01-31 07:42

    Use Comprehensions:

    iex(1)> string_key_map = %{"foo" => "bar", "hello" => "world"}
    %{"foo" => "bar", "hello" => "world"}
    
    iex(2)> for {key, val} <- string_key_map, into: %{}, do: {String.to_atom(key), val}
    %{foo: "bar", hello: "world"}
    
    0 讨论(0)
提交回复
热议问题