Access data from object

Hey there, how can I access the data here in this object map

[
  %{
    "definition" => "Bent like a knee; jointed",
    "pronunciation" => "Jenikulate",
    "word" => "Geniculate"
  }
]

like I want to access word

case HTTPoison.Base.get!("#{@base_url}") do
      %HTTPoison.Response{body: body, status_code: 200} ->
        Poison.decode!(body)

      %HTTPoison.Response{status_code: status_code} when status_code > 399 ->
        IO.inspect(status_code, label: "STATUS_CODE")
        :error

      _ ->
        raise Ranwords.Error
    end
1 Like

Might not be the best solution… But it works:

Poison.decode!(body)
|> List.first()
|> Map.get("word")
2 Likes

Is it also possible to get “word + meaning”?

The problem you have is that your map is inside the list so first you have to extract the map from the list as @jdj_dk suggested. To access any map key, the best way would be to pattern match it to a variable and then access it from there.

map = 
   Poison.decode!(body)
   |> List.first()

Then you can easily access and key you want.

map["word"]
map["definition"]
1 Like

This post was flagged by the community and is temporarily hidden.

No it won’t. You can do it this way;

def map_from_list(list) do
   list
   |> List.first()
end

def get_map_keys() do
   # list = Poison.decode!(body) code

   map = map_from_list(list)
   
   # now you can access any map key you need
   map{"word"]  <> map["definition"]
end
1 Like

This post was flagged by the community and is temporarily hidden.