Maxximiliann

Maxximiliann

Reusing atomized JSON binary keys

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?

Marked As Solved

Maxximiliann

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!

Also Liked

al2o3cr

al2o3cr

If one of the elements of decoded_api_response has additional keys not present in the first one, naive_string_to_atom will 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:

iex(1)> Jason.decode("{\"24h\":0}", keys: :atoms!)
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not an already existing atom

    :erlang.binary_to_existing_atom("24h", :utf8)
    (elixir 1.13.4) lib/string.ex:2456: String.to_existing_atom/1
    (jason 1.3.0) lib/decoder.ex:322: Jason.Decoder.object/6
    (jason 1.3.0) lib/decoder.ex:55: Jason.Decoder.parse/2
al2o3cr

al2o3cr

The message in the ArgumentError is only the top half of the output from triggering that error in iex - the other half is from __STACKTRACE__:

iex(4)> try do                                      
...(4)>   Jason.decode("{\"24h\":0}", keys: :atoms!)
...(4)> rescue                                      
...(4)>   error ->                                  
...(4)>     __STACKTRACE__
...(4)> end
warning: variable "error" is unused (if the variable is not meant to be used, prefix it with an underscore)
  iex:7

[
  {:erlang, :binary_to_existing_atom, ["24h", :utf8],
   [error_info: %{module: :erl_erts_errors}]},
  {String, :to_existing_atom, 1, [file: 'lib/string.ex', line: 2456]},
  {Jason.Decoder, :object, 6, [file: 'lib/decoder.ex', line: 322]},
  {Jason.Decoder, :parse, 2, [file: 'lib/decoder.ex', line: 55]},
  {:erl_eval, :do_apply, 7, [file: 'erl_eval.erl', line: 744]},
  {:erl_eval, :try_clauses, 10, [file: 'erl_eval.erl', line: 987]},
  {:elixir, :recur_eval, 3, [file: 'src/elixir.erl', line: 296]},
  {:elixir, :eval_forms, 3, [file: 'src/elixir.erl', line: 274]}
]

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement