Asking a question on StackOverflow

Someone asked a question on StackOverflow, which is quite difficult to address because it gives little details, and some of it are on an external site (something which is frowned upon at StackOverflow). Since I commented his post, here is an example on how the question should, in my opinion, been asked. Details about the API were deleted since they are irrelevant, the issue seems to be only with the data structure.

The question

I have a complicated data structure and I would like to access the location map which matches a given id. Here is an example of the structure:

{"items",
         [
           %{
             "clanLevel" => 11,
             "clanPoints" => 53415,
             "location" => %{
               "countryCode" => "FR",
               "id" => 32000006,
               "isCountry" => true,
               "name" => "France"
             },
             "members" => 50,
             "name" => "Jean"
           },
           %{
             "clanLevel" => 29,
             "clanPoints" => 53333,
             "location" => %{
               "countryCode" => "AF",
               "id" => 32000007,
               "isCountry" => true,
               "name" => "Afghanistan"
             },
             "members" => 48,
             "name" => "A.F.G کبیر"
           }
	 ]
}

I tried to access id with response["id"] but I get:

** (FunctionClauseError) no function clause matching in Access.get/3    
    
    The following arguments were given to Access.get/3:
    
        # 1
        {"items", [%{"clanLevel" => 11, "clanPoints" => 53415, "location" => %{"countryCode" => "FR", "id" => 32000006, "isCountry" => true, "name" => "France"}, "members" => 50, "name" => "Jean"}, %{"clanLevel" => 29, "clanPoints" => 53333, "location" => %{"countryCode" => "AF", "id" => 32000007, "isCountry" => true, "name" => "Afghanistan"}, "members" => 48, "name" => "A.F.G کبیر"}]}
    
        # 2
        "id"
    
        # 3
        nil
    
    Attempted function clauses (showing 5 out of 5):
    
        def get(%module{} = container, key, default)
        def get(map, key, default) when is_map(map)
        def get(list, key, default) when is_list(list) and is_atom(key)
        def get(list, key, _default) when is_list(list)
        def get(nil, _key, default)
    
    (elixir 1.12.2) lib/access.ex:283: Access.get/3
    t.exs:38: (file)
    (elixir 1.12.2) lib/code.ex:1261: Code.require_file/2

How to go deeper in the structure to find the location?

A possible solution

defmodule Search do
  # Returns a location map (or nil if id is not found)
  def get_location(response, id) do
    entry = Enum.find(elem(response, 1), fn e -> e["location"]["id"] == id end)
    entry["location"]
  end
end

ds = {"items",
         [
           %{
             "clanLevel" => 11,
             "clanPoints" => 53415,
             "location" => %{
               "countryCode" => "FR",
               "id" => 32000006,
               "isCountry" => true,
               "name" => "France"
             },
             "members" => 50,
             "name" => "Jean"
           },
           %{
             "clanLevel" => 29,
             "clanPoints" => 53333,
             "location" => %{
               "countryCode" => "AF",
               "id" => 32000007,
               "isCountry" => true,
               "name" => "Afghanistan"
             },
             "members" => 48,
             "name" => "A.F.G کبیر"
           }
	 ]
}

IO.inspect(Search.get_location(ds, 32000007))

elem is to ignore the first member of the tuple received. We can then use Enum.find on the internal list.