Maxximiliann
This proof of concept periodically polls data from a specific API before decoding the JSON. It should create a list of atoms from the binary keys it receives the first time it queries the API for reuse with each subsequent poll.
defmodule ProofOfConcept do
def naive_string_to_atom(raw_json) do
{:ok, decoded_api_response} = Jason.decode(raw_json)
root_keys =
decoded_api_response
|> List.first()
|> Map.keys()
nested_keys =
Enum.flat_map(
root_keys,
fn key ->
decoded_api_response
|> Enum.map(fn map_element ->
foo = Map.get(map_element, key)
case is_map(foo) do
true ->
Map.keys(foo)
false ->
:not_a_key
end
end)
|> Enum.reject(&(&1 == :not_a_key))
end
)
|> Enum.flat_map(& &1)
|> Enum.uniq()
extracted_keys =
(nested_keys ++ root_keys)
|> Enum.map(&String.to_atom(&1))
IO.inspect(extracted_keys, limit: :infinity)
end
def decode(raw_json) do
try do
Jason.decode(raw_json, keys: :atoms!)
rescue
_ ->
IO.puts("Converting JSON keys to atoms . . . ")
naive_string_to_atom(raw_json)
main()
end
end
def main() do
api_url = "https://wavescap.com/api/assets.json"
{:ok, api_response} = HTTPoison.get(api_url)
decode(api_response.body)
end
end
The problem:
Apparently the atomized keys are never retained in memory, requiring them to be recreated each time updated data is retrieved from the API.
iex(1)> ProofOfConcept.main
Converting JSON keys to atoms . . .
[:firstPrice_brln, :firstPrice_btc, :firstPrice_cnyn, :firstPrice_eth,
:firstPrice_eurn, :firstPrice_gbpn, :firstPrice_jpyn, :firstPrice_ngnn,
:firstPrice_rubn, :firstPrice_tryn, :firstPrice_uahn, :"firstPrice_usd-n",
:firstPrice_waves, :lastPrice_brln, :lastPrice_btc, :lastPrice_cnyn,
:lastPrice_eth, :lastPrice_eurn, :lastPrice_gbpn, :lastPrice_jpyn,
:lastPrice_ngnn, :lastPrice_rubn, :lastPrice_tryn, :lastPrice_uahn,
:"lastPrice_usd-n", :lastPrice_waves, :"24h_vol_brln", :"24h_vol_btc",
:"24h_vol_cnyn", :"24h_vol_eth", :"24h_vol_eurn", :"24h_vol_gbpn",
:"24h_vol_jpyn", :"24h_vol_ngnn", :"24h_vol_rubn", :"24h_vol_tryn",
:"24h_vol_uahn", :"24h_vol_usd-n", :"24h_vol_waves", :circulating, :data,
:excludedFromCirculating, :id, :name, :precision, :sender, :shortcode, :start,
:totalSupply, :trades, :website]
Converting JSON keys to atoms . . .
[:firstPrice_brln, :firstPrice_btc, :firstPrice_cnyn, :firstPrice_eth,
:firstPrice_eurn, :firstPrice_gbpn, :firstPrice_jpyn, :firstPrice_ngnn,
:firstPrice_rubn, :firstPrice_tryn, :firstPrice_uahn, :"firstPrice_usd-n",
:firstPrice_waves, :lastPrice_brln, :lastPrice_btc, :lastPrice_cnyn,
:lastPrice_eth, :lastPrice_eurn, :lastPrice_gbpn, :lastPrice_jpyn,
:lastPrice_ngnn, :lastPrice_rubn, :lastPrice_tryn, :lastPrice_uahn,
:"lastPrice_usd-n", :lastPrice_waves, :"24h_vol_brln", :"24h_vol_btc",
:"24h_vol_cnyn", :"24h_vol_eth", :"24h_vol_eurn", :"24h_vol_gbpn",
:"24h_vol_jpyn", :"24h_vol_ngnn", :"24h_vol_rubn", :"24h_vol_tryn",
:"24h_vol_uahn", :"24h_vol_usd-n", :"24h_vol_waves", :circulating, :data,
:excludedFromCirculating, :id, :name, :precision, :sender, :shortcode, :start,
:totalSupply, :trades, :website]
Converting JSON keys to atoms . . .
[:firstPrice_brln, :firstPrice_btc, :firstPrice_cnyn, :firstPrice_eth,
:firstPrice_eurn, :firstPrice_gbpn, :firstPrice_jpyn, :firstPrice_ngnn,
:firstPrice_rubn, :firstPrice_tryn, :firstPrice_uahn, :"firstPrice_usd-n",
:firstPrice_waves, :lastPrice_brln, :lastPrice_btc, :lastPrice_cnyn,
:lastPrice_eth, :lastPrice_eurn, :lastPrice_gbpn, :lastPrice_jpyn,
:lastPrice_ngnn, :lastPrice_rubn, :lastPrice_tryn, :lastPrice_uahn,
:"lastPrice_usd-n", :lastPrice_waves, :"24h_vol_brln", :"24h_vol_btc",
:"24h_vol_cnyn", :"24h_vol_eth", :"24h_vol_eurn", :"24h_vol_gbpn",
:"24h_vol_jpyn", :"24h_vol_ngnn", :"24h_vol_rubn", :"24h_vol_tryn",
:"24h_vol_uahn", :"24h_vol_usd-n", :"24h_vol_waves", :circulating, :data,
:excludedFromCirculating, :id, :name, :precision, :sender, :shortcode, :start,
:totalSupply, :trades, :website]
Converting JSON keys to atoms . . .
[:firstPrice_brln, :firstPrice_btc, :firstPrice_cnyn, :firstPrice_eth,
:firstPrice_eurn, :firstPrice_gbpn, :firstPrice_jpyn, :firstPrice_ngnn,
:firstPrice_rubn, :firstPrice_tryn, :firstPrice_uahn, :"firstPrice_usd-n",
:firstPrice_waves, :lastPrice_brln, :lastPrice_btc, :lastPrice_cnyn,
:lastPrice_eth, :lastPrice_eurn, :lastPrice_gbpn, :lastPrice_jpyn,
:lastPrice_ngnn, :lastPrice_rubn, :lastPrice_tryn, :lastPrice_uahn,
:"lastPrice_usd-n", :lastPrice_waves, :"24h_vol_brln", :"24h_vol_btc",
:"24h_vol_cnyn", :"24h_vol_eth", :"24h_vol_eurn", :"24h_vol_gbpn",
:"24h_vol_jpyn", :"24h_vol_ngnn", :"24h_vol_rubn", :"24h_vol_tryn",
:"24h_vol_uahn", :"24h_vol_usd-n", :"24h_vol_waves", :circulating, :data,
:excludedFromCirculating, :id, :name, :precision, :sender, :shortcode, :start,
:totalSupply, :trades, :website]
Converting JSON keys to atoms . . .
[:firstPrice_brln, :firstPrice_btc, :firstPrice_cnyn, :firstPrice_eth,
:firstPrice_eurn, :firstPrice_gbpn, :firstPrice_jpyn, :firstPrice_ngnn,
:firstPrice_rubn, :firstPrice_tryn, :firstPrice_uahn, :"firstPrice_usd-n",
:firstPrice_waves, :lastPrice_brln, :lastPrice_btc, :lastPrice_cnyn,
:lastPrice_eth, :lastPrice_eurn, :lastPrice_gbpn, :lastPrice_jpyn,
:lastPrice_ngnn, :lastPrice_rubn, :lastPrice_tryn, :lastPrice_uahn,
:"lastPrice_usd-n", :lastPrice_waves, :"24h_vol_brln", :"24h_vol_btc",
:"24h_vol_cnyn", :"24h_vol_eth", :"24h_vol_eurn", :"24h_vol_gbpn",
:"24h_vol_jpyn", :"24h_vol_ngnn", :"24h_vol_rubn", :"24h_vol_tryn",
:"24h_vol_uahn", :"24h_vol_usd-n", :"24h_vol_waves", :circulating, :data,
:excludedFromCirculating, :id, :name, :precision, :sender, :shortcode, :start,
:totalSupply, :trades, :website]
After each new API poll, why aren’t the atoms that were previously generated being reused?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
If one of the elements of
decoded_api_responsehas additional keys not present in the first one,naive_string_to_atomwill not see them and the bug you’re seeing would happen.The error message here should be helpful - on OTP 25, it is explicit about what string failed:
Maxximiliann
Per your suggestion I made the following update:
Outcome:
It seems it’s trying to atomize new lines? Am I reading this right?
al2o3cr
The message in the
ArgumentErroris only the top half of the output from triggering that error iniex- the other half is from__STACKTRACE__:Maxximiliann
The API is not universally constructed, as it turns out, so some records have keys that are absent from others. I was able to deduce what was going wrong and rewrite the code to accommodate for it thanks to being able to examine the error stacktrace. Thanks for the tip!
Btw, please elaborate on a few of the points you made here when you have a moment to spare, thanks again!