yeroc

yeroc

Extracting All Items of List Inside Map

Hello all!

A bit stumped on interpreting how to parse and extract my desired data from an API’s response with HTTPoison and Poison. I think I’m missing something simple.

Let’s say I want all the applicant names from “https://data.sfgov.org/resource/jjew-r69b.json?” .

In IEx-

iex(1)> res = HTTPoison.get!  "https://data.sfgov.org/resource/jjew-r69b.json?"

%HTTPoison.Response{
  body: "[{\"dayorder\":\"3\",\"dayofweekstr\":\"Wednesday\",\"starttime\":\"1PM\",\"endtime\":\"3PM\",\"permit\":\"19MFF-00079\",\"location\":\"1377 FELL ST\",\"locationdesc\":\"1:05pm-3:00pm\",\"optionaltext\":\"Cold Truck: Pre-packaged sandwiches, snacks, fruit, various beverages\"...

iex(2)> body = Poison.decode! res.body, keys: :atoms   

%{
    addr_date_create: "2017-07-31T15:11:15.000",
    addr_date_modified: "2017-08-01T10:49:12.000",
    applicant: "Bonito Poke",
    block: "3736",
    cnn: "107000",
    coldtruck: "N",
    dayofweekstr: "Wednesday",
    dayorder: "3",...

iex(3)> (hd body).applicant

"Liang Bai Ping"

As you can see from the JSON in the link, this is indeed the head, coming back to me in a string as I want after a bit of manipulation through HTTPoison and Poison. How would I go about calling and returning all of the heads and not just the head or tail?

Enum.map gives me nil

iex(4)> Enum.map(body, & &1["applicant"])
[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
 nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
 nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
 nil, nil, ...]

Thanks for any help :slight_smile:

Marked As Solved

cloudytoday

cloudytoday

In your Poison.decode! call you’re specifying keys: :atoms which constructs the resulting maps’ keys as atoms and not strings, but in your map call you’re providing a function which retrieves the field as it was a string (& &1[“applicant”]). I would recommend removing the keys: :atoms from the Poison.decode! call. Alternatively, you can do the same but with & &1.applicant.

Also Liked

cloudytoday

cloudytoday

Oh I just meant that the alternative solution, if, for some reason, you really need them as atoms, would be to keep the keys: :atoms in the Poison call, and to just change the function in the Enum.map call so that it retrieves the atoms via map.atom_field syntax (it’s equivalent to some_map[:atom_key]). And if you don’t need them as atoms, then you can simply remove the keys: :atoms from the Poison call. :slight_smile:

yeroc

yeroc

Ah yes! Thanks for clarifying! :yellow_heart:

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
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
pmjoe
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement