Access literal dynamic key in decoded Poison response

Hi all, I have a problem where I’ve decoded a json response with Poison but the API returns a dynamic key name which matches the query I’ve made. First I make a request with HTTPoison:

# postal_code_query == "90210"
url = ""
headers = [apikey: "secret"]
params = [codes: postal_code_query, country: country_alpha2]
{:ok, response} = HTTPoison.get(url, headers, params: params)
{:ok, decoded} = Poison.decode(response.body)

Which works fine but when I get the response the key is not constant but matches the param postal_code_query above("90210"):

Poison.decode(response.body): {:ok,
   "query" => %{"codes" => ["90210"], "country" => "US"},
   "results" => %{
     "90210" => [
         "city" => "Beverly Hills",
         "country_code" => "US",
         "latitude" => "34.09010000",
         "longitude" => "-118.40650000",
         "postal_code" => "90210",
         "province" => "Los Angeles",
         "province_code" => "037",
         "state" => "California",
         "state_code" => "CA"

I’ve tried various ways to get the values for the key "90210" but nothing I’ve tried so far seems to work:

    IO.inspect(postal_code_query, label: "postal_code_query")
    IO.inspect(decoded[postal_code_query], label: "decoded[postal_code_query]")
    IO.inspect(decoded[Atom.to_string(:postal_code_query)], label: "decoded[Atom.to_string()]")
    IO.inspect(Map.get(decoded, postal_code_query), label: "Map.get(decoded, postal_code_query)")


postal_code_query: "90210"
decoded[postal_code_query]: nil
decoded[Atom.to_string()]: nil
Map.get(decoded, postal_code_query): nil

Any suggestions?


When you’re digging around in mysterious data, Map.keys/1 is a good place to start.

Thanks, that helped me where I needed to go. I was CodedWhileTired(again). so it was simply decoded["results"][postal_code_query]