40

I have a keyword list of Ecto changeset errors I'd like to convert to a map so that the Poison JSON parser can correctly output a list of validation errors in the JSON format.

I get a list of errors as follows:

[:topic_id, "can't be blank", :created_by, "can't be blank"]

...and I'd like to get a map of errors like so:

%{topic_id: "can't be blank", created_by: "can't be blank"}

Alternatively, if I could convert it to a list of strings, I could use that as well.

What is the best way to accomplish either of these tasks?

3 Answers 3

69

What you have there isn't a keyword list, it is just a list with every odd element representing a key and every even element representing a value.

The difference is:

[:topic_id, "can't be blank", :created_by, "can't be blank"] # List
[topic_id: "can't be blank", created_by: "can't be blank"]   # Keyword List

A keyword list can be turned into a map using Enum.into/2

Enum.into([topic_id: "can't be blank", created_by: "can't be blank"], %{})

Since your data structure is a list, you can convert it using Enum.chunk_every/2 and Enum.reduce/3

[:topic_id, "can't be blank", :created_by, "can't be blank"]
|> Enum.chunk_every(2)
|> Enum.reduce(%{}, fn ([key, val], acc) -> Map.put(acc, key, val) end)

You can read more about Keyword lists at http://elixir-lang.org/getting-started/maps-and-dicts.html

5
  • 3
    I can't try it out right now, but I have a feeling that you should be able to reduce the last example to [...] |> Enum.chunk(2) |> Enum.into(%{}) Jul 21, 2015 at 22:31
  • Ah, I missed difference between a list and a keyword list. Thanks!
    – Eric Budd
    Jul 24, 2015 at 21:05
  • I also am having the same problem as OP, only I believe Changeset has changed its implementation. ATOW the changeset errors is a keyword list, e.g. [field_1: {"is invalid", [type: Ecto.UUID]}, field_2: {"can't be blank", []}]. I was able to convert to a map using this answer's Enum.into approach with error_map = Enum.into(changeset.errors, %{}). As a map, I could then get my message with {err_msg, _type} = Map.fetch!(error_map, :field_1).
    – user4275029
    Jun 13, 2016 at 12:22
  • This works for me: [topic_id: "can't be blank", created_by: "can't be blank"] |> Enum.into(%{}). Seemingly no need for the chunk.
    – Ryan Bigg
    Oct 16, 2017 at 5:09
  • @RyanBigg Well spotted. I was using the wrong data structure in the last example.
    – Gazler
    Oct 16, 2017 at 14:50
11

Another way to do this is by combining Enum.chunk/2 with Enum.into/3. For example:

[:topic_id, "can't be blank", :created_by, "can't be blank"]
|> Enum.chunk(2)
|> Enum.into(%{}, fn [key, val] -> {key, val} end)
6

Yet another way to do that is by using of list comprehension:

iex> list = [:topic_id, "can't be blank", :created_by, "can't be blank"]
iex> map = for [key, val] <- Enum.chunk(list, 2), into: %{}, do: {key, val}
%{created_by: "can't be blank", topic_id: "can't be blank"}

Besides you can convert your list to keyword list:

iex> klist = for [key, val] <- Enum.chunk(list, 2), do: {key, val}
[topic_id: "can't be blank", created_by: "can't be blank"]

It could be also useful in some cases.

You can read more about this use case at http://elixir-lang.org/getting-started/comprehensions.html#results-other-than-lists

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.