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
Last Post!
maz
Thanks, that helped me where I needed to go. I was CodedWhileTired(again). so it was simply decoded["results"][postal_code_query]
0
Popular in Questions
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
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security










