maz

maz

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 = "https://app.zipcodebase.com/api/v1/search"
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)")

yields:

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

Any suggestions?

regards,
Michael

Marked As Solved

al2o3cr

al2o3cr

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

Last Post!

maz

maz

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

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement