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
When you’re digging around in mysterious data, Map.keys/1 is a good place to start.
Popular in Questions
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
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
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New








