I am new to elixir and I am trying to get some text from a very nested map.
So I am doing a get request to this link and I am decoding it with Jason.decode.
What I want to do is iterate over it and get every text value (sections->0->content->0->text).
In the end I just want it to be a big string of all the text values
(the link can always change so there might be more maps etc)
Thanks in advance!
You can use
Enum
with the pipe operator|>
to enumerate the data structure and extract the parts you want.For example:
A breakdown:
Map.get("sections")
selects the contents of thesections
.Enum.reject(...)
ignores empty sections.Enum.flat_map
iterates over the sections, gets thecontents
of each section, transforms it using the inner function, and then flatten the result into a single list.Enum.filter(...)
only process contents whosetype
property istext
orparagraph
.Enum.map
extracts thetext
property from each contents map.Enum.join
joins the resulting list of strings with a newline character.