Skip to content

Instantly share code, notes, and snippets.

@zubairshokh
Last active February 1, 2017 17:23
Show Gist options
  • Save zubairshokh/4bae1ed45ecfadd94f5ae62d0d7e6dd5 to your computer and use it in GitHub Desktop.
Save zubairshokh/4bae1ed45ecfadd94f5ae62d0d7e6dd5 to your computer and use it in GitHub Desktop.
Extract key, value from nested maps or list of maps
defmodule Nested do
def get_inner_element(input) do
Enum.at(input,0)
|> __MODULE__.get_map(["accessConfigs"] )
|>Enum.at(0)
|> __MODULE__.get_element_from_map("natIP")
end
def get_map(map,[head|tail]) do
map[head] |> get_map tail
end
def get_map(map,[]), do: map
def get_element_from_map(map,key) do
map[key]
end
end
########
input = [%{"accessConfigs" => [%{"kind" => "compute#accessConfig",
"name" => "External NAT", "natIP" => "146.148.23.208",
"type" => "ONE_TO_ONE_NAT"}]}]
IO.inspect get_inner_element(input)
### result= "146.148.23.208"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment