Last active
February 1, 2017 17:23
-
-
Save zubairshokh/4bae1ed45ecfadd94f5ae62d0d7e6dd5 to your computer and use it in GitHub Desktop.
Extract key, value from nested maps or list of maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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